Create new column in pandas dataframe by calculation from previous index columns

Viewed 924

Hi I am new and learning pandas for data analysis. I have 2 columns data

A   B
1   2   
2   3
3   4
4   5

I want to create a third column C which result would be calculated by column B , by subtracting upper value with current one and dividing by current.

A   B   C
1   2   
2   3   0.33
3   4   0.25
4   5   0.2

for example first row value for C column is empty because there is no value above 2 .

0.33 = > 3 - 2 / 3 , 
0.25 = > 4 - 3 / 4 ,
0.2 = > 5 - 4 / 5 and so on

I am stuck while getting the upper value of current column. Need help how to achieve that.

3 Answers
Related