简体   繁体   English

在 python 中为组保留值

[英]Retain Value in python for groups

I have a data which looks like below.我有一个如下所示的数据。

country item    week    stock   FCST
A        1       1       105    3
A        1       2       105    6
A        1       3       105    9
A        1       4       105    4
A        1       5       105    7
A        1       6       105    4
A        1       7       105    7

the task i wish to perform is assigning closing stock of the current week as the opening stock of next week.我希望执行的任务是将本周的期末库存指定为下周的期初库存。 in the above table my stock qty was 105 at very beginning, based on the forecast(fcst column) it is decreasing and assigning as closing stock of the same week.在上表中,我的库存数量一开始是 105,根据预测(fcst 列)它正在减少并指定为同一周的收盘库存。 now the closing stock should become opening stock for next coming week.现在收盘库存应该成为下周的开盘库存。

I have done the same in SAS using retain statement.我在 SAS 中使用保留语句做了同样的事情。 I do not have any idea how replicate the same in python.我不知道如何在 python 中复制相同的内容。

Also to make a note this operation to be performed for every country-Item combination.还要记下要为每个国家/地区-项目组合执行的此操作。 (We can not always shift the value to take as opening stock, AS new item might have different current stock). (我们不能总是将价值转变为开盘库存,因为新项目可能有不同的当前库存)。

Can anyone help me out on the same.任何人都可以帮助我。

My Output should look like below table.我的输出应如下表所示。

country item    week    stock   FCST    OPENING_STOCK   CLOSING_STK
A        1       1       105      3          105           102
A        1       2       105      6          102            96
A        1       3       105      9           96            87
A        1       4       105      4           87            83
A        1       5       105      7           83            76
A        1       6       105      4           76            72
A        1       7       105      7           72            65

Please click on above link to see the desired output.请单击上面的链接以查看所需的输出。

Thanks in advance.提前致谢。

Now it works.现在它起作用了。 combine_first is used to fill gaps in df.opening combine_first 用于填补 df.opening 中的空白

import pandas as pd

df = pd.DataFrame({
'country': {0: 'A', 1: 'A', 2: 'A', 3: 'A', 4: 'A', 5: 'A', 6: 'A', 7: 'A', 8: 'A', 9: 'A', 10: 'A', 11: 'A', 12: 'A', 13: 'B', 14: 'B', 15: 'B', 16: 'B', 17: 'B', 18: 'B', 19: 'B', 20: 'B', 21: 'B', 22: 'B'}, 
'item': {0: 1, 1: 1, 2: 1, 3: 1, 4: 1, 5: 1, 6: 1, 7: 2, 8: 1, 9: 2, 10: 2, 11: 1, 12: 1, 13: 1, 14: 1, 15: 1, 16: 2, 17: 3, 18: 2, 19: 1, 20: 2, 21: 1, 22: 3},
'week': {0: 1, 1: 2, 2: 3, 3: 4, 4: 5, 5: 6, 6: 7, 7: 7, 8: 8, 9: 8, 10: 9, 11: 9, 12: 10, 13: 1, 14: 2, 15: 3, 16: 3, 17: 3, 18: 4, 19: 4, 20: 5, 21: 5, 22: 5},
'stock': {0: 105, 1: 105, 2: 105, 3: 105, 4: 105, 5: 105, 6: 105, 7: 94, 8: 105, 9: 94, 10: 94, 11: 105, 12: 105, 13: 100, 14: 100, 15: 100, 16: 200, 17: 300, 18: 200, 19: 100, 20: 200, 21: 100, 22: 300},
'FCST': {0: 3, 1: 6, 2: 9, 3: 4, 4: 7, 5: 4, 6: 7, 7: 2, 8: 1, 9: -5, 10: 2, 11: 8, 12: 6, 13: 2, 14: 6, 15: 8, 16: 3, 17: 7, 18: 8, 19: 9, 20: 3, 21: 5, 22: 6}
})
df_new = pd.DataFrame(columns=df.columns)
groups = df.groupby(["country", "item"])
df["closing"] = df.stock - groups.FCST.cumsum()
df["opening"] = groups.closing.shift(1)
df["opening"] = df["opening"].combine_first(df.stock)

Outputs:输出:

   country  item  week  stock  FCST  closing  opening
0        A     1     1    105     3      102    105.0
1        A     1     2    105     6       96    102.0
2        A     1     3    105     9       87     96.0
3        A     1     4    105     4       83     87.0
4        A     1     5    105     7       76     83.0
5        A     1     6    105     4       72     76.0
6        A     1     7    105     7       65     72.0
7        A     2     7     94     2       92     94.0
8        A     1     8    105     1       64     65.0
9        A     2     8     94    -5       97     92.0
10       A     2     9     94     2       95     97.0
11       A     1     9    105     8       56     64.0
12       A     1    10    105     6       50     56.0
13       B     1     1    100     2       98    100.0
14       B     1     2    100     6       92     98.0
15       B     1     3    100     8       84     92.0
16       B     2     3    200     3      197    200.0
17       B     3     3    300     7      293    300.0
18       B     2     4    200     8      189    197.0
19       B     1     4    100     9       75     84.0
20       B     2     5    200     3      186    189.0
21       B     1     5    100     5       70     75.0
22       B     3     5    300     6      287    293.0

The code i have used to solve the issue is pasted below.我用来解决问题的代码粘贴在下面。

    df.sort_values(by=['ITM_CD','Country','WEEK'],inplace=True)
    df['CONCAT']=df['Country']+df['ITM_CD']

#CALCULATE BEGINING STOCK EVERY WEEK
    df['TMP1']=1
    grouper = (df["CONCAT"]!= df["CONCAT"].shift()).cumsum()   
    df["WEEK_NO"] = df.groupby(grouper)['TMP1'].cumsum() 
    df["FCST1"] = df.groupby(grouper)['FCST'].cumsum() 
 
    result = df.CURR_STCK_TOT - df.FCST1
    df["CLOSING"] = result
    df["CLOSING"] = np.where(df["CLOSING"]<0,0,df["CLOSING"])    
    df["OPENING"] = np.where(df["WEEK_NO"]==1,df["STOCK"],result.shift(1))
    df["OPENING"] = np.where(df["OPENING"]<0,0,df["OPENING"])  

Also i have done some extra manipulation like making all negative values 0.我还做了一些额外的操作,比如将所有负值设为 0。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM