Reputation: 311
I am quite new to pandas and I have a dataframe as follows,
year A B
0 2000 101 20
1 2001 102 10
2 2002 103 5
3 2003 250 2
4 2004 500 4
I want to create new column ( column C) by dividing the column B row value by the row value just below it. As follows,
year A B C
0 2000 101 20 2
1 2001 102 10 2
2 2002 103 5 2.5
3 2003 250 2 0.5
4 2004 500 4
Not sure how to do it ( think for loop helps), Thanks
Upvotes: 0
Views: 1624
Reputation: 402824
Use div
+ shift
:
df['C'] = df.B / df.B.shift(-1)
df
year A B C
0 2000 101 20 2.0
1 2001 102 10 2.0
2 2002 103 5 2.5
3 2003 250 2 0.5
4 2004 500 4 NaN
Upvotes: 2