1

I have a dataframe, containing one column with prices. whats the best way to create a column calculating the rate of return between two rows (leaving the first or last Null).

For example the data frame looks as follows:

    Date        Price
2008-11-21    23.400000
2008-11-24    26.990000
2008-11-25    28.000000
2008-11-26    25.830000

Trying to add a column as follows:

    Date        Price       Return
2008-11-21    23.400000     0.1534
2008-11-24    26.990000     0.0374
2008-11-25    28.000000    -0.0775
2008-11-26    25.830000      NaN

Where the calculation of return column as follows:

Return Row 0 = Price Row 1 / Price Row 0 - 1

Should i for loop, or is there a better way?

2
  • 2
    Can you post raw data, code, your attempts and desired result. Commented Nov 25, 2016 at 11:02
  • Updated accordingly. Thx @EdChum. Commented Nov 25, 2016 at 11:10

1 Answer 1

3

You can use shift to shift the rows and then div to divide the Series against itself shifted:

In [44]:
df['Return'] = (df['Price'].shift(-1).div(df['Price']) - 1)
df

Out[44]:
         Date  Price    Return
0  2008-11-21  23.40  0.153419
1  2008-11-24  26.99  0.037421
2  2008-11-25  28.00 -0.077500
3  2008-11-26  25.83       NaN
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.