python - Pandas:撤消累积(例如累积和)

标签 python excel pandas cumulative-sum

我收到了累积数字的数据。有没有一种聪明的方法来积累数据,所以我每个月都有它而不是堆叠在一起?

(在此处查看示例 xlsx:https://docs.google.com/spreadsheets/d/1yELrJdZmi3CFJccYSi5U6GGDW-Awp5spHDnsDyshBe0/edit?usp=sharing。)

示例输入:

Date    SalesRep    itemA   itemB
01-01-2018  Jakob   5       10
01-01-2018  Adomas  10      20
01-01-2018  Thomas  15      30
01-02-2018  Jakob   50      30
01-02-2018  Adomas  100     40
01-02-2018  Thomas  150     65

期望的输出:
Date    SalesRep    itemA   itemB
01-01-2018  Jakob   5       10
01-01-2018  Adomas  10      20
01-01-2018  Thomas  15      30
01-02-2018  Jakob   45      20
01-02-2018  Adomas  90      20
01-02-2018  Thomas  135     35

此致,

普热梅斯瓦夫

附言更新

如果数据不是每个月都在增加,情况会怎样?

示例输入:
Date    SalesRep    itemA   itemB
01-01-2018  Jakob   5       10
01-01-2018  Adomas  10      20
01-01-2018  Thomas  15      30
**01-02-2018    Jakob   50      30**
01-02-2018  Adomas  100     40
01-02-2018  Thomas  150     65
**01-03-2018    Jakob   50      30**
01-03-2018  Adomas  102     60
01-03-2018  Thomas  155     75

Jakob 的情况如何,他每个月都没有增加,然后您的解决方案不起作用?我可以以某种方式指定参数来检查它并仅在有变化时减去吗?

最佳答案

您可以按销售代表分组并获取逐行差异。然后将数据集重新合并在一起。

import pandas as pd

df = pd.DataFrame({
    'Date': ['01-01-2018', '01-01-2018', '01-01-2018', '01-02-2018', '01-02-2018', '01-02-2018'],
    'SalesRep': ['Jakob', 'Adomas', 'Thomas', 'Jakob', 'Adomas', 'Thomas',],
    'itemA': [5, 10, 15, 50, 100, 150],
    'itemB': [10, 20, 30, 30, 40, 65]})

df_diff = df.groupby('SalesRep').diff().fillna(0).astype(int)
df.loc[:, ['itemA', 'itemB']] = df_diff.where(df_diff, df.loc[:, ['itemA', 'itemB']])

df
# returns:
         Date SalesRep  itemA  itemB
0  01-01-2018    Jakob      5     10
1  01-01-2018   Adomas     10     20
2  01-01-2018   Thomas     15     30
3  01-02-2018    Jakob     45     20
4  01-02-2018   Adomas     90     20
5  01-02-2018   Thomas    135     35

关于python - Pandas:撤消累积(例如累积和),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52275262/

相关文章:

python - Django 'if and' 模板

python - python3 open "x"模式有什么作用?

python - AWS Lambda : Unable to import module 'package/file' : No module named 'util'

python - 在类构造函数中初始化 tensorflow session

python - Excel - 通过 Python 刷新电源查询数据

python - matplotlib plt.figsize() 参数不适用于 pandas DataFrame 图

Excel:如何根据数据表创建动态数据验证列表

regex - R中的Excel RegEx函数

python-2.7 - 对 pandas 数据帧重新采样并计算实例数

python - 连接 1 行数据帧时的 Pandas 索引行为