这适用于按 5 年块对我的数据进行分组:
dg = df.groupby((df.index//5)*5).mean()['matches-ratio']
dg.plot()
这可以按性别(一列)对我的数据进行分组:
dg = df.groupby(['gender'])['matches-ratio']
dg.plot()
但我似乎无法同时按性别和 5 年大块分组。我试过
dg = df.groupby(['gender', (df.index//5)*5]).mean()['matches-ratio']
之类的东西,但这会产生奇怪的结果,其中日期按性别 (???) 和 5 年组分组,因此 x 轴标记为“性别,日期”。像这样链接它们:dg = df.groupby(['gender'])['matches-ratio']
dg = dg.groupby((df.index//5)*5).mean()
dg.plot()
给出
AttributeError: Cannot access callable attribute 'groupby' of 'SeriesGroupBy' objects, try using the 'apply' method
。如何在不同的轴上分组两次? (日期 = x 轴,'匹配比率' = y 轴) 最佳答案
你可能想用 unstack 来跟进你的 groupby
(df.groupby(['gender', (df.index//5)*5])
.mean()['matched-ratio']
.unstack()
.plot())
这将为每个性别创建一个单独的行。
关于python - 在 Pandas 中,如何在两个不同的轴上分组两次?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36073600/