1.我想计算一下excel中我的“性别”栏中有多少男性和女性。
我试过了sex_value = df.groupby("sex").size()
,但其中一些在中有空格。例如"F "
和"F"
(与"M" "M "
相同)
如果一切都像"M" or "F"
,我会使用:
sex_value = df.groupby("sex").size()
Output:
sex
F 37
F 27
M 40
M 31
dtype: int64
就我而言,应该是这样的
sex_value_female = df[(df['sex']=='F') & (df['sex'] == 'F ')].sum()
sex_value_male = df[(df['sex']=='M') & (df['sex'] == 'M ')].sum()
但它不起作用
2。平均值也存在同样的问题。
#mean value of brainweight and bodyweight for males and females
mean = df.groupby('sex').agg({'bodywt': 'mean', 'brainwt': 'mean'})
Output:
bodywt brainwt
sex
F 19.696216 410.059459
F 21.262963 440.122222
M 21.669750 410.030000
M 22.870968 433.709677
让我们做脱衣舞来摆脱空白
相关问题 更多 >
编程相关推荐