简体   繁体   中英

Pandas assign a column value basis previous row different column value

I have a df like this:

在此处输入图像描述

and the resultDF I want needs to be like this:

在此处输入图像描述

So except first row I want Supply value to be added with Available value of previous row and then subtract it with order value. Eg 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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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