【问题标题】:How to combine the sum of one column based on multiple unique values of another column in Pandas DataFrame?如何根据 Pandas DataFrame 中另一列的多个唯一值组合一列的总和?
【发布时间】:2019-11-10 04:46:01
【问题描述】:

我希望根据 Store_name 列中的唯一值合并 Sales_volume($) 列的总和。不确定这是否可能,但这是我的 DataFrame:

    Store_name  Sales_volume($) Date    
0   store 167   1.00               2019-06-03
1   store 167   4.00               2019-06-03
2   store 177   3.37               2019-06-03
3   store 177   2.14               2019-06-03
4   store 216   7.96               2019-06-03
5   store 216   1.99               2019-06-03

我想要的输出:

    Store_name  Sales_volume($) Date    
0   store 167   5.00               2019-06-03
1   store 177   5.51               2019-06-03
2   store 216   9.95               2019-06-03

谢谢!

【问题讨论】:

  • 使用:df.groupby('Store_name').agg({'Sales_volume($)':'sum', 'Date':'first'}).reset_index()

标签: python pandas function dataframe data-manipulation


【解决方案1】:

据我了解,您希望按 Store_name 对数据进行分组并获得商店的销售额总和: df.groupby('Store_name').sum() 或者,如果您想在每个日期都有商店的销售额: df.groupby(['Store_name','Date']).sum()

【讨论】:

    猜你喜欢
    • 2020-09-26
    • 2022-11-23
    • 1970-01-01
    • 2012-10-15
    • 1970-01-01
    • 2021-11-10
    • 2019-12-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多