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:
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