Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Values diplicated in ln(x_t/Xt_1) as growth rate

I am trying get a growth rate in a dataframe using: ln(x1_t/x1_t-1) but I can´t get because I have repetead values.

This is a example:

data = [['t1',9],['t2',16],['t3', 20], ['t4', 28]]
df = pd.DataFrame(data,columns=['Name','Cash_Flow'],dtype=float)

for row in range(len(df)):
    df['ln(CF_t/CF_t-1)'] = mt.log(df.iloc[row, 1] / df.iloc[row-1, 1])

The result is:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel


   Name Cash_Flow   ln(CF_t/CF_t-1)
0   t1  9.0         0.336472
1   t2  16.0        0.336472
2   t3  20.0        0.336472
3   t4  28.0        0.336472

>Solution :

You are overwriting the entire ln(CF_t/CF_t-1) column in each iteration of the loop. You need to update only the current row value for ln(CF_t/CF_t-1)

import pandas as pd
import numpy as np

data = [['t1',9],['t2',16],['t3', 20], ['t4', 28]]
df = pd.DataFrame(data,columns=['Name','Cash_Flow'],dtype=float)

df['ln(CF_t/CF_t-1)'] = np.log(df['Cash_Flow'] / df['Cash_Flow'].shift(1))

print(df)

output:

 Name  Cash_Flow  ln(CF_t/CF_t-1)
0   t1        9.0              NaN
1   t2       16.0         0.575364
2   t3       20.0         0.223144
3   t4       28.0         0.336472
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading