简体   繁体   English

Pandas 根据上一行不同的列值分配列值

[英]Pandas assign a column value basis previous row different column value

I have a df like this:我有一个这样的df:

在此处输入图像描述

and the resultDF I want needs to be like this:我想要的 resultDF 需要是这样的:

在此处输入图像描述

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.例如,对于 resultDF 中的第 3 行,Supply 值 (2306) 是通过将来自 resultDF 的可用值(145,第 2 行)和来自 df 的 Supply 值(2161,第 3 行)相加来生成的。 And then simply Available value is calculated using Supply - Order.然后使用供应 - 订单简单地计算可用价值。 Can anyone help me with how to generate resultDF.任何人都可以帮助我如何生成 resultDF。

Use cumsum :使用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:使用cumsum计算正确的值:

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: 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

暂无
暂无

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

相关问题 将值与上一行中的值进行比较,然后将值分配给另一列(熊猫)-如何加快速度? - Compare a value to the value in the previous row and assign a value to another column (Pandas) - how to speed up? Pandas数据框架如何根据特定组和上一行值为列分配值 - Pandas dataframe how to assign value to a column basing on a specific group and previous row value Pandas 列取决于其先前的值(行)? - Pandas column that depends on its previous value (row)? 用 Pandas 中的前一行列填充当前行和列值 - Fill current row and column value with previous row column in Pandas 根据其他列值创建Pandas Dataframe行 - Creating Pandas Dataframe row on the basis of other column value 从Pandas列中的当前行值中减去前一行的值 - Subtract previous row value from the current row value in a Pandas column 从不同行的列中为 pandas DataFrame 列分配值的最佳方法是什么? - What is the optimal way to assign a value to a pandas DataFrame column from a column in a different row? Python/Pandas:根据多个列/行值为列赋值 - Python/Pandas: assign a value to a column based on multiple column/row values Python:如何填充依赖于前一个值(前一行)的Pandas列? - Python: How to populate Pandas column that depends on the previous value (previous row)? Pandas:如果满足条件,则将列的值替换为前一行的值 - Pandas: Replace value of column with previous row value if condition is met
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM