【问题标题】:How to sum the total value of the same year? (Pandas)当年的总价值如何求和? (熊猫)
【发布时间】:2022-01-03 14:56:10
【问题描述】:

我正在用 pandas 学习 Python 数据分析

我有一个游戏销售数据框,看起来像这样:

(此数据不真实,仅供提问)

Name                Year    Publisher   Total Sales
GTA V               2013    Rockstar    133000
Super Mario Bros    1985    Nintendo    430500
GTA VI              2025    Rockstar    86000
RDR 3               2025    Rockstar    129030
Super Mario Sister  1985    Nintendo    308900
Super Mario End     2000    Nintendo    112100

然后我删除名称并使用此命令按发布者名称对其进行分组:

df.drop(columns='Name', inplace=True)
df.groupby(['Publisher','Year','Total Sales']).sum().reset_index()

数据框现在看起来像这样:

Publisher   Year    Total Sales
Nintendo    1985    308900
Nintendo    1985    430500
Nintendo    2000    112100
Rockstar    2013    133000
Rockstar    2025    129030
Rockstar    2025    86000

这很好,但我想将同一出版商同一年的总销售额相加

我希望数据框看起来像这样:

Publisher   Year    Total Sales
Nintendo    1985    739400
Nintendo    2000    86000
Rockstar    2013    129030
Rockstar    2025    215030

有没有办法做到这一点?

这是我的 df 代码:

data = {'Name':['GTA V','Super Mario Bros','GTA VI','RDR 3','Super Mario Sister','Super Mario End'],'Year':['2013','1985','2025','2025','1985','2000'],
        'Publisher':['Rockstar','Nintendo','Rockstar','Rockstar','Nintendo','Nintendo'],'Total Sales':['133000','430500','86000','129030','308900','112100']}

df = pd.DataFrame(data)
df

【问题讨论】:

    标签: python pandas dataframe filter


    【解决方案1】:

    使用pivot_table:

    >>> df.pivot_table('Total Sales', ['Year', 'Publisher'], aggfunc='sum').reset_index()
    
       Year Publisher  Total Sales
    0  1985  Nintendo       739400
    1  2000  Nintendo       112100
    2  2013  Rockstar       133000
    3  2025  Rockstar       215030
    

    注意:如果Total Sales 列包含字符串,则将其转换为int(或float):

    >>> df.astype({'Total Sales': int}).pivot_table(...)
    

    【讨论】:

      【解决方案2】:
      import pandas as pd
      
      data = {'Name':['GTA V','Super Mario Bros','GTA VI','RDR 3','Super Mario Sister','Super Mario End'],'Year':['2013','1985','2025','2025','1985','2000'],
              'Publisher':['Rockstar','Nintendo','Rockstar','Rockstar','Nintendo','Nintendo'],'Total Sales':['133000','430500','86000','129030','308900','112100']}
      
      df = pd.DataFrame(data)
      df['Total Sales'] = df['Total Sales'].astype(int)
      
      
      df.groupby(['Year', 'Publisher'])['Total Sales'].agg('sum').reset_index()
      

      【讨论】:

        【解决方案3】:

        这是一种方法:

        df.drop(columns='Name', inplace=True)
        df['Total Sales'] = pd.to_numeric(df['Total Sales'])
        df2 = df.groupby(['Publisher','Year']).sum().reset_index()
        df2
        

        【讨论】:

        • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
        猜你喜欢
        • 2020-07-18
        • 2018-10-03
        • 1970-01-01
        • 2020-03-28
        • 1970-01-01
        • 2021-01-06
        • 1970-01-01
        • 1970-01-01
        • 2017-07-03
        相关资源
        最近更新 更多