2024-06-29 00:50:49 发布
网友
我试图计算性别和类型两列下的平均重量,因为df如下:
sex | type | weight M Obese 305 F Normal 100 M Underweight 105
你想这么做吗?你知道吗
import pandas as pd df = pd.DataFrame({'sex': ['M', 'F', 'M'], 'type':['obese', 'Normal' ,'Underweight'], 'weight': [305, 100, 105]}) print(df[df['type'] == 'obese']['weight'].mean())
首先,我不认为你需要filter()函数,它做的事情和你想象的不同。其次,您总是希望先选择或“筛选”,然后再进行计算。你知道吗
希望这有帮助。你知道吗
你在这里用错了filter。将要保留的内容传递给like参数:
filter
like
df.groupby(['sex','type'])['weight'].mean().filter(like='Obese')
输出:
sex type M Obese 305 Name: weight, dtype: int64
你想这么做吗?你知道吗
首先,我不认为你需要filter()函数,它做的事情和你想象的不同。其次,您总是希望先选择或“筛选”,然后再进行计算。你知道吗
希望这有帮助。你知道吗
你在这里用错了
filter
。将要保留的内容传递给like
参数:输出:
相关问题 更多 >
编程相关推荐