Home > Net >  Pandas assign a column value basis previous row different column value
Pandas assign a column value basis previous row different column value

Time:12-01

I have a df like this:

enter image description here

and the resultDF I want needs to be like this:

enter image description here

So except first row I want Supply value to be added with Available value of previous row and then subtract it with order value. E.g. for row 3 in resultDF, Supply value (2306) is generated by adding Available value (145, row 2) from resultDF and Supply value (2161, row 3) from df. And then simply Available value is calculated using Supply - Order. Can anyone help me with how to generate resultDF. Thanks in advance!

CodePudding user response:

Use cumsum:

df["Available"] = df["Supply"].cumsum() - df["Order"].cumsum()
df["Supply"] = df["Available"]   df["Order"]

>>> df
  product   Month  Order  Supply  Available
0  xx-xxx  202107    718  1531.0      813.0
1    None  202108    668   813.0      145.0
2    None  202109   5030  2306.0    -2724.0
3    None  202110    667 -2724.0    -3391.0

CodePudding user response:

Use cumsum to compute right values:

Assuming:

  • you want to fix your rows per product
  • your rows are already ordered by (product, month)
# Setup
data = {'Product': ['xx-xxx', 'xx-xxx', 'xx-xxx', 'xx-xxx'],
        'Month': [202107, 202108, 202109, 202110],
        'Order': [718, 668, 5030, 667],
        'Supply': [1531, 0, 2161, 0],
        'Available': [813, -668, -2869, -667]}
df = pd.DataFrame(data)

df[['Supply', 'Available']] = df.groupby('Product').apply(lambda x: \
    pd.DataFrame({'Supply': x['Order']   x['Supply'].cumsum() - x['Order'].cumsum(),
                  'Available': x['Supply'].cumsum() - x['Order'].cumsum()}))

Output:

>>> df
  Product   Month  Order  Supply  Available
0  xx-xxx  202107    718    1531        813
1  xx-xxx  202108    668     813        145
2  xx-xxx  202109   5030    2306      -2724
3  xx-xxx  202110    667   -2724      -3391
  • Related