这可以按5年组将我的数据分组:
dg = df.groupby((df.index//5)*5).mean()['matches-ratio']
dg.plot()
Run Code Online (Sandbox Code Playgroud)
这可以按性别对我的数据进行分组(一列):
dg = df.groupby(['gender'])['matches-ratio']
dg.plot()
Run Code Online (Sandbox Code Playgroud)
但我似乎无法按性别和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()
Run Code Online (Sandbox Code Playgroud)
给AttributeError: Cannot access callable attribute 'groupby' of 'SeriesGroupBy' objects, try using the 'apply' method。如何在不同的轴上分组两次?(日期= x轴,“匹配比率” = y轴)
您可能想跟进您的小组讨论
(df.groupby(['gender', (df.index//5)*5])
.mean()['matched-ratio']
.unstack()
.plot())
Run Code Online (Sandbox Code Playgroud)
这将为每个性别创建一个单独的行。