admin管理员组

文章数量:1334133

I have a dataframe like below

  Name     Value
====================
   A        2400 
   B        -400
   C        400
   D        600

And i need the df to be in the below format

  Name    Lower_Value   Upper_Value
======================================
   A          0              2400 
   B        2400             -400
   C        2000             400
   D        2400             0

So basically, the actual values should be Upper_Values and the Lower_values should be the addition of both the Lower_Values and Upper_Values from the previous row

so far, i have tried something like,

df['Upper_Value']=df['Value']
df['Lower_Value'] = df.upper_value.shift(1).fillna(0)
df['Lower_Value'] = df['Lower_Value'] + df['Upper_Value']

Any help or suggestion is much appreciated.

Thanks,

I have a dataframe like below

  Name     Value
====================
   A        2400 
   B        -400
   C        400
   D        600

And i need the df to be in the below format

  Name    Lower_Value   Upper_Value
======================================
   A          0              2400 
   B        2400             -400
   C        2000             400
   D        2400             0

So basically, the actual values should be Upper_Values and the Lower_values should be the addition of both the Lower_Values and Upper_Values from the previous row

so far, i have tried something like,

df['Upper_Value']=df['Value']
df['Lower_Value'] = df.upper_value.shift(1).fillna(0)
df['Lower_Value'] = df['Lower_Value'] + df['Upper_Value']

Any help or suggestion is much appreciated.

Thanks,

Share Improve this question asked Nov 20, 2024 at 9:55 SM079SM079 7553 gold badges11 silver badges32 bronze badges 4
  • Why is the last value of Upper_Value zero and not 600? – PaulS Commented Nov 20, 2024 at 10:06
  • The data is kind of being loaded to something like a waterfall chart, so the lower value of first data and the upper value of the last data has to be 0 – SM079 Commented Nov 21, 2024 at 5:23
  • @SM079 - Answer was edited. – jezrael Commented Nov 21, 2024 at 6:29
  • @SM079: mine is also edited. – PaulS Commented Nov 21, 2024 at 11:02
Add a comment  | 

2 Answers 2

Reset to default 2

A possible solution, whose steps are:

  • First, it uses assign method to add the new columns. It first creates a new column, Lower_Value, by shifting the Value column down using shift with fill_value=0, and then computes the cumulative sum with cumsum.

  • Next, it modifies the Upper_Value column by applying a lambda function that utilizes where to retain original Value entries except for the last one, which is replaced with 0.

(df.assign(
    Lower_Value=df['Value'].shift(fill_value=0).cumsum(),
    Upper_Value=lambda x: x['Value'].where(x.index != x.index[-1], 0)))

Output:

  Name  Value  Lower_Value  Upper_Value
0    A   2400            0         2400
1    B   -400         2400         -400
2    C    400         2000          400
3    D    600         2400            0

Use Series.shift with fill_value parameter with Series.cumsum and reasssign new column with DataFrame.pop with set last value to 0:

out = df.assign(Lower_Value = df['Value'].shift(fill_value=0).cumsum(),
                Upper_Value = df.pop('Value'))

out.iloc[-1, out.columns.get_loc('Upper_Value')] = 0

print (out)
  Name  Lower_Value  Upper_Value
0    A            0         2400
1    B         2400         -400
2    C         2000          400
3    D         2400            0

Or:

out = df.assign(Lower_Value = df['Value'].shift(fill_value=0).cumsum(),
                Upper_Value = df.pop('Value').mul(df.index != df.index[-1]))

print (out)
  Name  Lower_Value  Upper_Value
0    A            0         2400
1    B         2400         -400
2    C         2000          400
3    D         2400            0

How it working:

print (df.assign(shifted=df['Value'].shift(fill_value=0),
                 lower = df['Value'].shift(fill_value=0).cumsum()))
  Name  Value  shifted  lower
0    A   2400        0      0
1    B   -400     2400   2400
2    C    400     -400   2000
3    D    600      400   2400

本文标签: pythonAdding values from 2 cells in the previous row in to current row in dataframeStack Overflow