import pandas as pd import numpy as np import warnings warnings.filterwarnings("ignore") pd.options.plotting.backend = "plotly" #从csv文件获取数据 data = pd.read_csv('testPandasShit.csv', sep='\s+') pd.set_option('display.max_rows',None) pd.set_option('display.max_columns',None) print("原始数据:\n{0}".format(data['close'])) print("shift(1)后数据\n{0}".format(data['close'].shift(1))) ret = np.log(data["close"]) - np.log(data["close"].shift(1)) print("收益率对数:\n{0}\n".format(ret))
数据:
date open close high low volume money 2022-08-16 4498.0 4498.0 4498.0 4498.0 1.0 6.747000e+04 2022-08-17 4486.0 4500.0 4511.0 4486.0 7.0 4.729950e+05 2022-08-18 4433.0 4391.0 4436.0 4391.0 10.0 6.618900e+05 2022-08-19 4373.0 4352.0 4387.0 4344.0 29.0 1.893450e+06
标签:计算,python,data,08,4498.0,2022,pd,close,收益率 From: https://www.cnblogs.com/justinzhang/p/17456427.html