[英]Pandas find the mean of DataFrame based on a conditional across multiple columns
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.我正在尝试查找 20 岁以下女性的平均体重,我有以下 DataFrame,我已经将年龄转换为 int 并将体重转换为 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:我试过
df.groupby(['male','age'])['weight'].mean()[False]
但它只返回如下内容:
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?我如何过滤它,以便它汇总所有 20 岁以下女性的体重,然后取平均值?
There's no need for groupby unless I'm misunderstanding.除非我误解,否则不需要 groupby 。 You can filter the dataframe based on your conditions then take the mean of the weight column.
您可以根据您的条件过滤 dataframe 然后取权重列的平均值。
df.loc[(~df["male"]) & (df["age"] < 20), "weight"].mean()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.