Merging/combining two dataframes with different frequency time series indexes in Pandas?(在 Pandas 中合并/组合两个具有不同频率时间序列索引的数据帧?)
问题描述
使用熊猫 0.15.1.假设我有以下两个数据框:
Using pandas 0.15.1. Suppose I have the following two dataframes:
daily
2014-11-20 00:00:00 Rain
2014-11-21 00:00:00 Cloudy
2014-11-22 00:00:00 Sunny
.
minutely
2014-11-20 12:45:00     51
2014-11-20 12:46:00     43
2014-11-20 12:47:00     44
...
2014-11-21 12:45:00     44
2014-11-21 12:46:00     46
2014-11-21 12:47:00     48
...
2014-11-22 12:45:00     38
2014-11-22 12:46:00     32
2014-11-22 12:47:00     37
我想组合这两个数据框,以便将日期值传播到具有相应日期的每一分钟行.
I'd like to combine the two dataframes such that the day values get propagated to each minute row that have the corresponding day.
由于分钟行在 00:00:00 实际上没有数据,我不希望该时间包含在结果数据框中.期望的输出:
And since the minute rows do not actually have data at 00:00:00 I do not want that time included in the resulting dataframe. Desired output:
2014-11-20 12:45:00     51  Rain
2014-11-20 12:46:00     43  Rain
2014-11-20 12:47:00     44  Rain
...
2014-11-21 12:45:00     44  Cloudy
2014-11-21 12:46:00     46  Cloudy
2014-11-21 12:47:00     48  Cloudy
...
2014-11-22 12:45:00     38  Sunny
2014-11-22 12:46:00     32  Sunny
2014-11-22 12:47:00     37  Sunny
我怎样才能做到这一点?我需要使用合并、连接或连接吗?
How can I achieve this? Do I need to use merge, concat, or join?
推荐答案
开头:
>>> left
                     minutely
2014-11-20 12:45:00        51
2014-11-20 12:46:00        43
2014-11-20 12:47:00        44
2014-11-21 12:45:00        44
2014-11-21 12:46:00        46
2014-11-21 12:47:00        48
2014-11-22 12:45:00        38
2014-11-22 12:46:00        32
2014-11-22 12:47:00        37
>>> right
             daily
2014-11-20    Rain
2014-11-21  Cloudy
2014-11-22   Sunny
你可以这样做:
>>> left['day'] = left.index.date
>>> right.index = right.index.date
>>> left.join(right, on='day', how='left')
                     minutely         day   daily
2014-11-20 12:45:00        51  2014-11-20    Rain
2014-11-20 12:46:00        43  2014-11-20    Rain
2014-11-20 12:47:00        44  2014-11-20    Rain
2014-11-21 12:45:00        44  2014-11-21  Cloudy
2014-11-21 12:46:00        46  2014-11-21  Cloudy
2014-11-21 12:47:00        48  2014-11-21  Cloudy
2014-11-22 12:45:00        38  2014-11-22   Sunny
2014-11-22 12:46:00        32  2014-11-22   Sunny
2014-11-22 12:47:00        37  2014-11-22   Sunny
                        这篇关于在 Pandas 中合并/组合两个具有不同频率时间序列索引的数据帧?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 Pandas 中合并/组合两个具有不同频率时间序列
				
        
 
            
        - 使用Heroku上托管的Selenium登录Instagram时,找不到元素';用户名'; 2022-01-01
 - 分析异常:路径不存在:dbfs:/databricks/python/lib/python3.7/site-packages/sampleFolder/data; 2022-01-01
 - python check_output 失败,退出状态为 1,但 Popen 适用于相同的命令 2022-01-01
 - 沿轴计算直方图 2022-01-01
 - 如何将一个类的函数分成多个文件? 2022-01-01
 - padding='same' 转换为 PyTorch padding=# 2022-01-01
 - 如何在 Python 的元组列表中对每个元组中的第一个值求和? 2022-01-01
 - pytorch 中的自适应池是如何工作的? 2022-07-12
 - python-m http.server 443--使用SSL? 2022-01-01
 - 如何在 python3 中将 OrderedDict 转换为常规字典 2022-01-01
 
				
				
				
				