简体   繁体   中英

Math operations (addition and power) in python Dataframe

I currently have this dataframe

Date
1987-01-26   -0.629487
1988-01-25    0.552159
1989-01-23    0.247890
1990-01-22    0.294639
1991-01-21    0.400885
1992-01-20    0.099296
1993-01-18    0.256380

The second column contains 3-year return. I want to replace the existing column,the 3 year return (R), with the equivallent 1 year return (r), such that: Equation

I believe need numpy.power :

df['R'] = np.power(1 + df['Col'], 1/3) - 1

Or numpy.cbrt :

df['R'] = np.cbrt(1 + df['Col']) - 1

print (df)
         Date       Col         R
0  1987-01-26 -0.629487 -0.281763
1  1988-01-25  0.552159  0.157832
2  1989-01-23  0.247890  0.076611
3  1990-01-22  0.294639  0.089891
4  1991-01-21  0.400885  0.118925
5  1992-01-20  0.099296  0.032060
6  1993-01-18  0.256380  0.079047

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM