如何解决如何基于python中的列标签计算均值和中位数
我有一个很大的数据框,显示如下:
price type status
2 shoes none
3 clothes none
6 clothes none
3 shoes none
4 shoes none
6 shoes none
2 clothes none
3 shoes none
6 clothes none
8 clothes done
基本上,无论何时编写“状态”,我都希望基于“类型”计算平均值和中位数。 到目前为止,我首先根据状态“完成”将一个组设为一个组,然后像下面的脚本一样计算该组的平均值和中位数:
g = df['status'].eq('done').iloc[::-1].cumsum().iloc[::-1]
grouper = df.groupby(g)
df_statistics = grouper.agg(
mean = ('price','mean'),median = ('price','median')
)
df_freq = df.groupby(g).apply(lambda x: x['price'].value_counts().idxmax())
如何为“类型”添加另一个参数,因此脚本也将根据“类型”估算每个组的中位数。
谢谢
解决方法
我认为您需要传递列名才能列出,然后传递到groupby
:
grouper = df.groupby([g,'type'])
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。