按多列分组并获得总和和计数

时间:2019-10-13 20:43:39

标签: python pandas dataframe

我正在尝试创建一个数据框,可以按年份,季节和联赛显示5个常见禁令字符。 我最初的df看起来像这样:

    League  Year    Season  ban_1   ban_2   ban_3   ban_4   ban_5
0   NALCS   2015    Spring  Rumble  Kassadin Lissandra NaN NaN
1   NALCS   2015    Spring  Tristana Leblanc Nidalee NaN NaN
2   NALCS   2015    Spring  Kassadin Sivir  Lissandra NaN NaN
3   NALCS   2015    Spring  RekSai  Janna   Leblanc NaN NaN
4   NALCS   2015    Spring  JarvanIV Lissandra Kassadin NaN NaN

最后,我希望它看起来像这样:

Year    Season  League  Top 5 bans
2015    Spring  EULCS   [(Zed, 49), (Kassadin, 39), (Cassiopeia, 34), (RekSai, 33), (Nidalee, 30)]

在这一点上,我一直在试图使之有意义,所以我尝试了这一点:

bans_info.groupby(['Year','Season', 'League', 'ban_1', 'ban_2', 'ban_3', 'ban_4', 'ban_5',]).sum()

这:

bans_info.groupby(['Year','Season','League'])。ban_1.value_counts() 但最后还是没有得到它,我尝试将其分开制作,但变得太乱了

b1 = bans_info.groupby(['Year', 'Season', 'League']).ban_1.value_counts()
b2 = bans_info.groupby(['Year', 'Season', 'League']).ban_2.value_counts()
b12 = pd.merge(b1, b2, how='outer', on ='Year')

1 个答案:

答案 0 :(得分:1)

您需要使用.agg,然后传入列名和函数的字典。

You can find more detail here.