假设我有两个DataFrames
:intraday
每分钟有一行,而daily
每天有一行。
如何在intraday['some_val']
值(日期成分)等于some_val
值(日期成分)的daily['some_val']
行中添加intraday.index
列,并在其中添加daily.index
?
最佳答案
根据以下设置,
intraday = pd.DataFrame(index=pd.date_range('2016-01-01', '2016-01-07', freq='T'))
daily = pd.DataFrame(index=pd.date_range('2016-01-01', '2016-01-07', freq='D'))
daily['some_val'] = np.arange(daily.shape[0])
您可以从两个索引的日期部分创建一个列,然后在该列上合并
daily['date'] = daily.index.date
intraday['date'] = intraday.index.date
daily.merge(intraday)
date some_val
0 2016-01-01 0
1 2016-01-01 0
2 2016-01-01 0
3 2016-01-01 0
4 2016-01-01 0
... ... ...
8636 2016-01-06 5
8637 2016-01-06 5
8638 2016-01-06 5
8639 2016-01-06 5
8640 2016-01-07 6
或者,您可以利用自动索引对齐功能,并使用
fillna
。intraday['some_val'] = daily['some_val']
intraday.fillna(method='ffill', downcast='infer')
some_val
2016-01-01 00:00:00 0
2016-01-01 00:01:00 0
2016-01-01 00:02:00 0
2016-01-01 00:03:00 0
2016-01-01 00:04:00 0
... ...
2016-01-06 23:56:00 5
2016-01-06 23:57:00 5
2016-01-06 23:58:00 5
2016-01-06 23:59:00 5
2016-01-07 00:00:00 6
请注意,这仅在
daily
索引的时间分量为00:00
时有效。关于python - 将每日值合并到日内DataFrame中,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41271258/