1 回答
TA贡献1810条经验 获得超4个赞
如果需要将列中的前几天除以close列中的实际天数low:
df['prev'] = df['Close'].shift(freq='1d').div(df['Low'])
print (df)
High Low Open Close Volume \
Date
2018-01-02 2695.889893 2682.360107 2683.729980 2695.810059 3367250000
2018-01-03 2714.370117 2697.770020 2697.850098 2713.060059 3538660000
2018-01-04 2729.290039 2719.070068 2719.310059 2723.989990 3695260000
2018-01-05 2743.449951 2727.919922 2731.330078 2743.149902 3236620000
2018-01-08 2748.510010 2737.600098 2742.669922 2747.709961 3242650000
2020-04-13 2782.459961 2721.169922 2782.459961 2761.629883 5274310000
2020-04-14 2851.850098 2805.100098 2805.100098 2846.060059 5567400000
2020-04-15 2801.879883 2761.540039 2795.639893 2783.360107 5203390000
2020-04-16 2806.510010 2764.320068 2799.340088 2799.550049 5179990000
2020-04-17 2879.219971 2830.879883 2842.429932 2874.560059 5792140000
Adj Close bcc prev
Date
2018-01-02 2695.810059 False NaN
2018-01-03 2713.060059 False 0.999273
2018-01-04 2723.989990 False 0.997790
2018-01-05 2743.149902 False 0.998559
2018-01-08 2747.709961 True NaN
2020-04-13 2761.629883 False NaN
2020-04-14 2846.060059 False 0.984503
2020-04-15 2783.360107 False 1.030606
2020-04-16 2799.550049 False 1.006888
2020-04-17 2874.560059 False 0.988933
如果需要除以今天Low:
#last datetime changed to today
print (df)
High Low Open Close Volume \
Date
2018-01-02 2695.889893 2682.360107 2683.729980 2695.810059 3367250000
2018-01-03 2714.370117 2697.770020 2697.850098 2713.060059 3538660000
2018-01-04 2729.290039 2719.070068 2719.310059 2723.989990 3695260000
2018-01-05 2743.449951 2727.919922 2731.330078 2743.149902 3236620000
2018-01-08 2748.510010 2737.600098 2742.669922 2747.709961 3242650000
2020-04-13 2782.459961 2721.169922 2782.459961 2761.629883 5274310000
2020-04-14 2851.850098 2805.100098 2805.100098 2846.060059 5567400000
2020-04-15 2801.879883 2761.540039 2795.639893 2783.360107 5203390000
2020-04-16 2806.510010 2764.320068 2799.340088 2799.550049 5179990000
2020-04-21 2879.219971 2830.879883 2842.429932 2874.560059 5792140000
Adj Close bcc
Date
2018-01-02 2695.810059 False
2018-01-03 2713.060059 False
2018-01-04 2723.989990 False
2018-01-05 2743.149902 False
2018-01-08 2747.709961 True
2020-04-13 2761.629883 False
2020-04-14 2846.060059 False
2020-04-15 2783.360107 False
2020-04-16 2799.550049 False
2020-04-21 2874.560059 False
today = pd.Timestamp.today().floor('D')
print (today)
2020-04-21 00:00:00
print (df.loc[today, 'Low'])
2830.8798829999996
df['prev'] = df['Close'].shift(freq='1d').div(df.loc[today, 'Low'])
print (df)
High Low Open Close Volume \
Date
2018-01-02 2695.889893 2682.360107 2683.729980 2695.810059 3367250000
2018-01-03 2714.370117 2697.770020 2697.850098 2713.060059 3538660000
2018-01-04 2729.290039 2719.070068 2719.310059 2723.989990 3695260000
2018-01-05 2743.449951 2727.919922 2731.330078 2743.149902 3236620000
2018-01-08 2748.510010 2737.600098 2742.669922 2747.709961 3242650000
2020-04-13 2782.459961 2721.169922 2782.459961 2761.629883 5274310000
2020-04-14 2851.850098 2805.100098 2805.100098 2846.060059 5567400000
2020-04-15 2801.879883 2761.540039 2795.639893 2783.360107 5203390000
2020-04-16 2806.510010 2764.320068 2799.340088 2799.550049 5179990000
2020-04-21 2879.219971 2830.879883 2842.429932 2874.560059 5792140000
Adj Close bcc prev
Date
2018-01-02 2695.810059 False NaN
2018-01-03 2713.060059 False 0.952287
2018-01-04 2723.989990 False 0.958380
2018-01-05 2743.149902 False 0.962241
2018-01-08 2747.709961 True NaN
2020-04-13 2761.629883 False NaN
2020-04-14 2846.060059 False 0.975538
2020-04-15 2783.360107 False 1.005362
2020-04-16 2799.550049 False 0.983214
2020-04-21 2874.560059 False NaN
添加回答
举报