【问题标题】:finding highest goals scoring teams from each season through groupby pandas通过 groupby pandas 找到每个赛季进球最多的球队
【发布时间】:2021-10-08 16:50:33
【问题描述】:

我有这样的数据

    id  season      team                 wins   losses  goals
0   0   2006-2007   Manchester United    28     5       83
1   1   2006-2007   Chelsea              24     3       64  
2   2   2006-2007   Liverpool            20     10      57
.
.
238 238 2017-2018   Stoke City           7      19      35  
239 239 2017-2018   West Bromwich Albion 6      19      31  

我正在努力寻找每个赛季进球最多的球队

目标输出

        team                season              
        
        Manchester United   2006-2007           83
        
        Manchester United   2007-2008           80
        
        Liverpool           2008-2009           77
        .
        .
        Manchester City     2017-2018           106

我试过了,

df.groupby(['team','season'])['goals'].sum().sort_values(ascending = False).head(10)

team               season   
Manchester City    2017-2018    106
Chelsea            2009-2010    103
Manchester City    2013-2014    102
Liverpool          2013-2014    101
Manchester City    2011-2012     93
Manchester United  2011-2012     89
Tottenham Hotspur  2016-2017     86
Manchester United  2012-2013     86
                   2009-2010     86
Chelsea            2016-2017     85
Name: goals, dtype: int64

这给了我一个球队在所有赛季中的最高总进球数。一年在这里重复,我不希望岁月重复。

【问题讨论】:

    标签: python pandas dataframe pandas-groupby


    【解决方案1】:

    如果您添加更多数据,则可以验证这一点。如果两支球队在一个赛季中取得同样高的进球,这将不起作用。

    df.sort_values('goals', ascending = False).groupby('season').first()
    
                id               team  wins  losses  goals
    season
    2006-2007    0  Manchester United    28       5   83.0
    2017-2018  238         Stoke City     7      19   35.0
    

    【讨论】:

    • 没有二队。每个赛季都提到了每支球队的统计数据。再仔细看看。
    • 这个数据集不是fixtures。这是球队的统计数据。
    • 谢谢,我设法从上面做的。 ---most_G = df.sort_values('goals', ascending = False).groupby('season').first() ---most_G[['team','goals']] ---但我想也可以查看更精确的解决方案。
    猜你喜欢
    • 1970-01-01
    • 2016-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-07
    • 1970-01-01
    • 1970-01-01
    • 2019-09-04
    相关资源
    最近更新 更多