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 |2 Answers
Reset to default 2A 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 theValue
column down usingshift
withfill_value=0
, and then computes the cumulative sum withcumsum
.Next, it modifies the
Upper_Value
column by applying a lambda function that utilizeswhere
to retain originalValue
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
版权声明:本文标题:python - Adding values from 2 cells in the previous row in to current row in dataframe - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742367373a2461559.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
Upper_Value
zero and not 600? – PaulS Commented Nov 20, 2024 at 10:06