简体   繁体   English

Python 3:pandas 组多索引和每个多索引 2 个单独列的平均值

[英]Python 3: pandas group multiindex and mean of 2 separate columns per multiindex

What code do I need to perform a mean of both drugs and health per session by type.我需要什么代码来按类型执行 session 的药物和健康的mean

在此处输入图像描述

df[['drugs', 'health', 'session', 'type']]

these all are currently columns.这些都是目前的专栏。 I though it ought to be indexed by both session (first) and then type .我虽然它应该被session (首先)和然后type索引。

grouped_S = grouped['session'].as_index=True
grouped_c = grouped['type'].as_index=True
grouped_dh = grouped['drugs', 'health'].mean().as_index=False

How do I correct this code to make it work: getting an average of drugs and health per session and by type?我如何更正此代码以使其工作:按 session 和类型获取药物和健康的平均数?

IIUC, try this: IIUC,试试这个:

df.groupby(['session', 'type'])[['drugs', 'health']].mean()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM