python - Python 3:pandas 组多索引和每个多索引 2 个单独列的平均值
问题描述
我需要什么代码来mean
按类型在每个会话中执行药物和健康。
df[['drugs', 'health', 'session', 'type']]
这些都是当前的列。我虽然它应该被session
(first) 和 then索引type
。
grouped_S = grouped['session'].as_index=True
grouped_c = grouped['type'].as_index=True
grouped_dh = grouped['drugs', 'health'].mean().as_index=False
如何更正此代码以使其正常工作:每次会话和按类型获取药物和健康的平均值?
解决方案
IIUC,试试这个:
df.groupby(['session', 'type'])[['drugs', 'health']].mean()
推荐阅读
- javascript - 选择当前选项卡打开的浏览器选项卡
- cmake - 可以在运行/调试配置中使用 CLion 宏吗?
- azure - Azure 应用服务自动缩放不正确的平均 CPU 评估
- shell - 为什么“ls -l”的输出会丢失换行符?
- c++ - 什么是“模板”
结构别名;”在 C++ 中? - html - XPath / XSLT:如何嵌套兄弟姐妹,直到特定兄弟姐妹进入新元素?
- javascript - 在 ngForm 中使用时未定义选择列表
- java - Spring boot,@ManyToMany,为join Table创建一个仓库
- java - 如何在 SpringBoot 2 上禁用 ErrorPageFilter
- google-chrome - 如何解决“您现在不能访问……因为它的证书已被吊销。”