Search⌘ K
AI Features

Challenge Solution Review

Explore how to group data by columns and filter rows in a DataFrame using Pandas. Learn to aggregate values like sum and mean within groups, enabling you to analyze subsets of data effectively and apply conditional selection for detailed insights.

Solution review - group by a single column

Python 3.5
import pandas as pd
df = pd.read_csv("raw_data.csv",
sep=",",
header=0)
male_total = df.groupby(["Gender"]).get_group("Male").sum()["Price"]
female_mean = df.groupby(["Gender"]).get_group("Female").mean()["Price"]
print("The total price of male group is {}.".format(male_total))
print("The mean price of female group is {}".format(female_mean))
print(male_total, female_mean)

From line 3 to line 5, the CSV file is loaded to the DataFrame object.

df.groupby(["Gender"]).get_group("M
...