1.Fill missing ratings
IntermediateProduct ratings are category = ["food", "food", "toys", "toys", "toys"] and rating = [4.0, None, 3.0, None, 5.0]. Fill each missing rating with the average rating of its own category and print the result.
Show hint
groupby("category")["rating"].transform("mean"), then fillna with it.
Show solution
import pandas as pd
df = pd.DataFrame({"category": ["food", "food", "toys", "toys", "toys"],
"rating": [4.0, None, 3.0, None, 5.0]})
df["rating"] = df["rating"].fillna(df.groupby("category")["rating"].transform("mean"))
print(df) category rating
0 food 4.0
1 food 4.0
2 toys 3.0
3 toys 4.0
4 toys 5.0