使用yfinance得到下面的df,我如何计算" day_delta ";带+/-%增量的列
symbol Datetime Value_Type Open_Value Day_Delta
AAPL 2021-09-20 09:30:00-04:00 Close 143.839996
AAPL 2021-09-20 10:00:00-04:00 Close 142.940002
AAPL 2021-09-20 10:30:00-04:00 Close 142.750000
AAPL 2021-09-20 11:00:00-04:00 Close 143.089905
AAPL 2021-09-20 11:30:00-04:00 Close 142.490494
解决方案:
-
将Datetime分隔为两列:Date和Time
symbol Date Time Open_Value AAPL 2021-09-20 09:30:00-04:00 143.800003
-
对'symbol'和'Date'进行排序并删除重复项
stock_day_open = stock_data.sort_values(['symbol', 'Date', 'Time', 'Open_Value'], ascending[True, True, True, True]) stock_day_open = stock_day_open.drop_duplicates(['symbol','Date'], keep='first')
-
合并到new_df添加列
-
计算'Day_Delta'