Challenge
Aufgabe
Swipe to start coding
Now you can solve a fairly simple task - read a synthetic dataset with profiles on a social network and create new features.
- Create a new feature
Age Binning
(like before) that bins the users' ages into age groups (e.g. 20-30, 30-40, 40-50, etc.). Like, 35 (int) -> 30-40 (str) - Create a second feature
Average Hours
that counts the average number of hours per week spent on social media by individual users
Lösung
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import pandas as pd
import numpy as np
# Read the dataset
dataset = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/9c23bf60-276c-4989-a9d7-3091716b4507/datasets/social_network.csv')
# Age binning
# HINT: use a function pd.cut()
dataset['Age Group'] = pd.cut(dataset['Age'], bins=[10, 20, 30, 40, 50, 60, np.inf], labels=['10-20', '20-30', '30-40', '40-50', '50-60', '60+'])
# Average hours per week
# HINT: use .iloc() method
dataset['Average Hours'] = dataset.iloc[:, 3:-1].mean(axis=1)
# Print the dataset
print(dataset)
War alles klar?
Danke für Ihr Feedback!
Abschnitt 5. Kapitel 6
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import pandas as pd
import numpy as np
# Read the dataset
dataset = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/9c23bf60-276c-4989-a9d7-3091716b4507/datasets/social_network.csv')
# Age binning
# HINT: use a function pd.cut()
dataset['Age Group'] = pd.___(dataset['Age'], bins=[10, 20, 30, 40, 50, 60, np.inf], ___=['10-20', '20-30', '30-40', '40-50', '50-60', '60+'])
# Average hours per week
# HINT: use .iloc() method
dataset['Average Hours'] = dataset.___[:, 3:-1].___(axis=1)
# Print the dataset
print(dataset)
Fragen Sie AI
Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen