本文介绍了使用 pandas 滚动差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在尝试使用Pandas滚动功能来计算下表中的滚动差异.我正在尝试在每月可用项目"列中生成值,但没有得到任何结果.请帮忙!

Hello I am trying to use Pandas rolling function to calculate a rolling difference on the table below. I am trying to produce the values in the Monthly available items column and not getting anywhere. Please help!

Item    Adds    Subtracts   Month   Monthly Available items
A       71       30          Jan       41
B       19        7          Jan       12
C       68       30          Jan       38
D       34       19          Jan       15
E       180      91          Jan       89
C       58       34          Feb       62
E       222      129         Feb       182
D       26       21          Feb       20
B       65       26          Feb       51
A       62       58          Feb       45

到目前为止的Python代码:

Python code so far:

rolling_triggers ['Adds'].rolling(window = 2).apply(lambda x:x [1]-x [0])

rolling_triggers['Adds'].rolling(window = 2).apply(lambda x: x[1] - x[0])

推荐答案

如果我正确理解了您的代码和注释,这将满足您的要求:

If I'm understanding your code and comments correctly, this will do what you want:

months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']

df['Month'] = pd.Categorical(df['Month'], categories = months)
df = df.sort_values(['Item', 'Month'])
df['Net_Items'] = df['Adds'] - df['Subtracts']
df['Monthly_Available_Items'] = df.groupby('Item')['Net_Items'].cumsum()
df = df.reset_index(drop = True)

它将返回:

  Item  Adds  Subtracts Month  Net_Items  Monthly_Available_Items
0    A    71         30   Jan         41                       41
1    A    62         58   Feb          4                       45
2    B    19          7   Jan         12                       12
3    B    65         26   Feb         39                       51
4    C    68         30   Jan         38                       38
5    C    58         34   Feb         24                       62
6    D    34         19   Jan         15                       15
7    D    26         21   Feb          5                       20
8    E   180         91   Jan         89                       89
9    E   222        129   Feb         93                      182

首先,将月份列转换为类别(因为按字母顺序,12月在1月之前,依此类推).接下来,将Net_Items计算为AddsSubtracts之间的差.最后,计算每个产品的累计总和.

First, convert the month column to a Categorical (because alphabetically, December is before January, etc). Next, calculate Net_Items as the difference between Adds and Subtracts. Finally calculate a cumulative sum for each of the products.

假设一月是第一个月,则直觉是添加了71个Item A并删除了30个,总共31个.接下来的一个月,添加了62个并删除了58个,因此前一个月的净额41中增加了4,而总运转额为45.

Assuming January is the first month, the intuition is that there were 71 of Item A added and 30 removed for a net total of 31. In the next month, 62 were added and 58 were removed, so the net total of 4 is added to the prior month's net total of 41 for a running total of 45.

这篇关于使用 pandas 滚动差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!