【发布时间】:2020-06-25 10:32:41
【问题描述】:
我有这行代码,它获取前一天的最后一个值并将其重复添加到第二天的新列中。工作正常。
df = df.join(df.resample('B', on='Date')['x'].last().rename('xnew'), on=pd.to_datetime((df['Date'] - pd.tseries.offsets.BusinessDay()).dt.date))
现在我需要类似的东西,但我无法让它工作。
我现在需要“打开”中当天的第一个值,并将该值复制到新列“打开”中的每一行中,每一天
我试过了,但它不起作用:
df = df.join(df.resample('B', on='Date')['Open'].last().rename('opening'), on=pd.to_datetime((df['Date'])))
错误:
ValueError: columns overlap but no suffix specified: Index(['opening'], dtype='object')
我怎样才能做到这一点?
与:
opening = df.resample('B', on='Date')['Open'].first()
Date
2019-06-20 2927.25
2019-06-21 2932.75
2019-06-24 2942.00
2019-06-25 2925.00
2019-06-26 2902.75
...
2020-06-17 3116.50
2020-06-18 3091.50
2020-06-19 3101.75
2020-06-22 3072.75
2020-06-23 3111.25
..我得到第一个值,我想要的输出是
Date Open opening
1 2020-06-24 07:00:00 3091.50 3111.25
2 2020-06-24 07:05:00 3092.50 3111.25
3 2020-06-24 07:10:00 3090.25 3111.25
4 2020-06-24 07:15:00 3089.75 3111.25
这是一些示例数据。对于此示例,时间现在是从 7:00 到 7:15:
Time Open
Date
2019-06-20 07:00:00 70000 2927.25
2019-06-20 07:05:00 70500 2927.00
2019-06-20 07:10:00 71000 2927.00
2019-06-20 07:15:00 71500 2926.75
2019-06-21 07:00:00 70000 2932.75
2019-06-21 07:05:00 70500 2932.25
2019-06-21 07:10:00 71000 2933.00
2019-06-21 07:15:00 71500 2930.75
2019-06-24 07:00:00 70000 2942.00
2019-06-24 07:05:00 70500 2941.50
2019-06-24 07:10:00 71000 2942.00
2019-06-24 07:15:00 71500 2941.50
2019-06-25 07:00:00 70000 2925.00
2019-06-25 07:05:00 70500 2925.75
2019-06-25 07:10:00 71000 2926.50
2019-06-25 07:15:00 71500 2926.00
2019-06-26 07:00:00 70000 2902.75
2019-06-26 07:05:00 70500 2903.00
2019-06-26 07:10:00 71000 2904.00
2019-06-26 07:15:00 71500 2904.25
【问题讨论】:
-
@Narendra Prasath 不,因为我不想加入两个数据框
-
@MarkT 你能提供一些样本数据吗?画出一个解决方案会更容易:)
-
@Hugolmn 瞧 :)