pandas :计算按另一列分组的列的平均值 [英] Pandas: compute the mean of a column grouped by another column

查看:85
本文介绍了 pandas :计算按另一列分组的列的平均值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有一个像这样的数据框:

Say I have a dataframe like this:

            gender     height      weight  C
2000-01-01    male  42.849980  157.500553  1
2000-01-02    male  49.607315  177.340407  1
2000-01-03    male  56.293531  171.524640  1
2000-01-04  female  48.421077  144.251986  2
2000-01-05    male  46.556882  152.526206  2
2000-01-06  female  68.448851  168.272968  1
2000-01-07    male  70.757698  136.431469  2
2000-01-08  female  58.909500  176.499753  3
2000-01-09  female  76.435631  174.094104  3
2000-01-10    male  45.306120  177.540920  2

如何计算 C 列分组的 height 列的平均值?这将产生3个不同的值: C = 1 的那些高度的平均值, C = 2 的那些高度的平均值,等等.

How could I compute the mean of the height column, grouped by column C? This would yield 3 different values: the mean of those heights with C=1, that of those with C=2, and so forth.

到目前为止,我尝试了此操作,但无济于事:

So far I tried this but to no avail:

df ['height'].mean(groupby ='C')

->返回 TypeError:mean()得到了意外的关键字参数'groupby'

推荐答案

您的语法错误, mean 没有 groupby arg,您要 groupby ,然后调用

Your syntax is incorrect, there is no groupby arg for mean, you want to groupby on the col of interest and then call mean on the column of interest:

In [11]:
df.groupby('C')['height'].mean()

Out[11]:
C
1    54.299919
2    52.760444
3    67.672566
Name: height, dtype: float64

这篇关于 pandas :计算按另一列分组的列的平均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆