使用Pandas Dataframe 合并日数据和5分钟数据

toiithl6  于 2023-01-01  发布在  其他
关注(0)|答案(1)|浏览(159)

我有两个Pandas数据框。一个包含每日数据(dailyDf),一个包含5分钟数据(fiveMinDf)。现在添加fiveMinDf的前2个条作为列添加到dailyDf中的每一行
埃格

DailyDf
           Open Close
01-01-2022  100  101
01-02-2022  102  103

fiveMinDf
                  Open   Close
01-01-2022-9.30  100.1   100.2
01-01-2022-9.35  100.3   100.4
01-01-2022-9:40  100.9   100.9
01-02-2022-9.30  100.6   100.7
01-02-2022-9.35  100.8   100.49
01-02-2022-9:40  100.9   100.9

合并DF

DailyDf
           Open Close open_5min_9.30 open_5min_9.35 close_5min_9.30 close_5min_9.35
01-01-2022  100  101.   100.1         100.3            100.2          100.4
01-02-2022  102  103    100.6          100.8          100.7          100.49
yiytaume

yiytaume1#

注解代码

# Extract date and time columns from the index
fiveMinDf[['date', 'time']] = fiveMinDf.index.str.rsplit('-', n=1).tolist()

# get the first two rows per date
s = fiveMinDf.groupby('date').head(2)

# Reshape from long to wide
s = s.pivot('date', 'time')

# flatten the multiindex columns
s.columns = s.columns.map('_'.join)

# Join with dailyDF on common date
MergedDF = DailyDf.join(s)

结果

Open  Close  Open_9.30  Open_9.35  Close_9.30  Close_9.35
01-01-2022   100    101      100.1      100.3       100.2      100.40
01-02-2022   102    103      100.6      100.8       100.7      100.49

相关问题