I am trying to find the average weight of females under 20, I have the following DataFrame, I have already converted age to int and weight to float.
age weight height male
39.0 88.636360 180.0 True
64.0 75.000000 155.0 False
17.0 100.000000 183.0 False
35.0 63.636364 170.0 True
18.0 70.454544 173.0 False
I've tried df.groupby(['male','age'])['weight'].mean()[False]
but it just returns something like:
age
18.0 64.225121
19.0 65.499535
20.0 67.855026
21.0 69.622658
22.0 69.376862
How can I filter it so that it aggregates the weight of all female under 20 then takes the average?
There's no need for groupby unless I'm misunderstanding. You can filter the dataframe based on your conditions then take the mean of the weight column.
df.loc[(~df["male"]) & (df["age"] < 20), "weight"].mean()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.