按月分组,对一列求和,对另一列求平均。

huangapple go评论58阅读模式
英文:

Group by month, sum one column and average another

问题

我有一个数据框,看起来像这样:

日期 游戏次数 评分
2019-05-23 8 22
2023-01-29 10 32

实际表格要长得多。我想按月份对表格进行分组(日期列是一个 DateTime 格式的列),并在这样做的同时,将游戏次数列相加,但将评分列求平均。基本上,每一行都会有一个月份,该月的游戏总次数和该月的平均评分。在日期列上分组的同时,如何执行这些不同的汇总操作呢?

英文:

I have the a dataframe that looks like the following:

Date Games Played Rating
2019-05-23 8 22
2023-01-29 10 32

The actual table is much longer. I want to group the table by month (the date column is a DateTime format column), and in doing so, sum together the games played column but average the rating column. Essentially, every row will have a month, total games played that month, and average rating for that month. How can I do these separate aggregations while still grouping by month in the date column.

答案1

得分: 1

尝试:

x = df.groupby(df['Date'].dt.month).agg({'Games Played': 'sum', 'Rating': 'mean'})
print(x)

打印出:

      Games Played  Rating
Date                      
1               13    18.5
5               11    21.0

使用的数据框:

        Date  Games Played  Rating
0 2019-05-23             8      22
1 2019-05-24             1      21
2 2019-05-25             2      20
3 2023-01-28             3       5
4 2023-01-29            10      32

如果要按分组:

x = df.groupby([df['Date'].dt.year, df['Date'].dt.month]).agg({'Games Played': 'sum', 'Rating': 'mean'})
print(x)
英文:

Try:

x = df.groupby(df['Date'].dt.month).agg({'Games Played': 'sum', 'Rating': 'mean'})
print(x)

Prints:

      Games Played  Rating
Date                      
1               13    18.5
5               11    21.0

DataFrame used:

        Date  Games Played  Rating
0 2019-05-23             8      22
1 2019-05-24             1      21
2 2019-05-25             2      20
3 2023-01-28             3       5
4 2023-01-29            10      32

If you want to group by year and month:

x = df.groupby([df['Date'].dt.year, df['Date'].dt.month]).agg({'Games Played': 'sum', 'Rating': 'mean'})
print(x)

答案2

得分: 1

使用 aggregate 方法:

df.groupby(df.Date.dt.month).aggregate(
    {'Games Played': 'sum', 'Rating': 'mean'})
英文:

Use aggregate

df.groupby(df.Date.dt.month).aggregate(
    {'Games Played': 'sum', 'Rating': 'mean'})

huangapple
  • 本文由 发表于 2023年2月8日 10:23:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/75380832.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定