Calculate Daily Returns with Pandas DataFrame

前端 未结 3 1722
不思量自难忘°
不思量自难忘° 2021-01-31 18:41

Here is my Pandas data frame:

prices = pandas.DataFrame([1035.23, 1032.47, 1011.78, 1010.59, 1016.03, 1007.95, 
              1022.75, 1021.52, 1026.11, 1027.04,         


        
3条回答
  •  北荒
    北荒 (楼主)
    2021-01-31 19:07

    Because operations will do alignment on index, you can convert one of the DataFrames to array:

    prices[:-1].values / prices[1:] - 1
    

    or

    prices[:-1] / prices[1:].values - 1
    

    depends on what the index of the result you want.

    or use shift() method:

    prices.shift(1) / prices - 1
    

    and:

    prices / prices.shift(1) - 1
    

提交回复
热议问题