【问题标题】:How to find sum of these values in Pandas? [duplicate]如何在 Pandas 中找到这些值的总和? [复制]
【发布时间】:2023-02-05 15:18:05
【问题描述】:

我正在尝试创建一个脚本来分析我的数据。这是我的问题。假设我有一个这样的 excel 文件。

Code Value
A1    20
B1    30
A1    15
C1    20
B1    20

我需要 Pandas 来做这个文件。并写一个excel文件。我有一个这样的excel文件。

A1 35
B1 50
C1 20

代码很像这样。

import pandas as pd

data = pd.read_excel("filename.xlsx")

Missing Part Here

x.to_excel(r'filename.xlsx', index = True, header=True)

我需要缺少的部分。已经非常感谢您的解决方案。

尝试自动化我的数据分析。期望添加我的脚本的一部分。

【问题讨论】:

  • 请注意,Stackoverflow 不是免费的编码服务,这是一个常见的误解。

标签: python pandas data-analysis sum-of-digits


【解决方案1】:

你要找的是groupby

>>> df
  Code  Value
0   A1     20
1   B1     30
2   A1     15
3   C1     20
4   B1     20

>>> df.groupby('Code', as_index=False)['Value'].sum()
  Code  Value
0   A1     35
1   B1     50
2   C1     20

请抽空阅读10 minutes to Pandas

【讨论】:

    猜你喜欢
    • 2019-11-27
    • 1970-01-01
    • 2015-03-28
    • 1970-01-01
    • 2020-10-09
    • 2020-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多