Search⌘ K
AI Features

Solution: Reshaping/Aggregations

Explore how to manipulate data in pandas by creating dummy variables, grouping data to calculate means, and building pivot tables. This lesson helps you understand reshaping methods and aggregation techniques to analyze categorical and numerical data efficiently.

We'll cover the following...

Solution 1

Look at the following dataset:

Age 8 10 15 15 72 20 25 30 35 60 68 70 25 85 15
Sex M M F F M M M M F F M F F M F

Create dummy columns on the Sex attribute.

Python 3.10.4
import pandas as pd
df=pd.DataFrame({
"Age": [8,10,15,15,72,20,25,30,35,60,68,7,25,85,15],
"Sex" : ['M','M','F','F','M','M','M','M','F','F','M','F','F','M','F']
})
encoded = pd.get_dummies(df["Sex"])
print(encoded)
  • Line 7: We use the get_dummies function to encode the Sex column in the data frame df into ...