首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >合并小时上的两个日期列

合并小时上的两个日期列
EN

Stack Overflow用户
提问于 2017-04-29 13:54:37
回答 1查看 53关注 0票数 2

我有两个不同的数据

天气:

代码语言:javascript
复制
    time                dayweather
0   2015-11-14 01:00:00   Clouds
1   2015-11-14 03:00:00   Clouds
2   2015-11-14 05:00:00   Clouds
3   2015-11-14 06:00:00   Clouds
4   2015-11-14 08:00:00   Clouds

速度:

代码语言:javascript
复制
    id            time                  machine_id    speed_gps_kph  latitude     longitude
0   14641931007   2015-11-15 17:46:40         10051         3       -36.725578    174.71482
1   14642568129   2015-11-15 18:12:41         10051        13       -36.769465    174.74159
2   14641876524   2015-11-15 17:44:30         10051        0        -36.723136    174.714432
3   14642262476   2015-11-15 18:00:47         10051        17       -36.747435    174.723397
4   14641991113   2015-11-15 17:49:43         10051        6        -36.72826     174.715083

我需要按时合并这两个数据,然后检查天气对数据的影响。所以我把这两个数据合并

代码语言:javascript
复制
dfs = [dataframe,weather]
checkweather = reduce(lambda left,right: pd.merge(left,right,how='left',on='time'), dfs)

但我的问题是,正如你所看到的,白天的天气数据似乎只是完全相同的时间。

那么,如果速度数据时间在2小时之间(每小时记录一些天气时间),那么如何在天气数据时间中显示白天的结果?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-04-29 14:05:24

使用merge_asof的解决方案

代码语言:javascript
复制
print (dataframe)
                 time dayweather
0 2015-11-14 01:00:00    Clouds1
1 2015-11-14 03:00:00    Clouds2
2 2015-11-14 05:00:00    Clouds3
3 2015-11-14 06:00:00    Clouds4
4 2015-11-14 08:00:00       Rain

print (weather)
            id                time  machine_id  speed_gps_kph   latitude  \
0  14641931007 2015-11-14 04:46:40       10051              3 -36.725578   
1  14642568129 2015-11-14 05:12:41       10051             13 -36.769465   
2  14641876524 2015-11-14 06:44:30       10051              0 -36.723136   
3  14642262476 2015-11-14 07:00:47       10051             17 -36.747435   
4  14641991113 2015-11-15 17:49:43       10051              6 -36.728260   

    longitude  
0  174.714820  
1  174.741590  
2  174.714432  
3  174.723397  
4  174.715083 
代码语言:javascript
复制
df = pd.merge_asof(weather, dataframe, on='time', tolerance=pd.Timedelta('2H'))
print (df)
            id                time  machine_id  speed_gps_kph   latitude  \
0  14641931007 2015-11-14 04:46:40       10051              3 -36.725578   
1  14642568129 2015-11-14 05:12:41       10051             13 -36.769465   
2  14641876524 2015-11-14 06:44:30       10051              0 -36.723136   
3  14642262476 2015-11-14 07:00:47       10051             17 -36.747435   
4  14641991113 2015-11-15 17:49:43       10051              6 -36.728260   

    longitude dayweather  
0  174.714820    Clouds2  
1  174.741590    Clouds3  
2  174.714432    Clouds4  
3  174.723397    Clouds4  
4  174.715083        NaN  

另一种解决方案是使用resampleffill (first是必要的,因为文本列)映射Series,然后maptimefloor截断。

代码语言:javascript
复制
m = dataframe.set_index('time').resample('H')['dayweather'].first().ffill()
weather['dayweather'] = weather['time'].dt.floor('H').map(m)
print (weather)
            id                time  machine_id  speed_gps_kph   latitude  \
0  14641931007 2015-11-14 04:46:40       10051              3 -36.725578   
1  14642568129 2015-11-14 05:12:41       10051             13 -36.769465   
2  14641876524 2015-11-14 06:44:30       10051              0 -36.723136   
3  14642262476 2015-11-14 07:00:47       10051             17 -36.747435   
4  14641991113 2015-11-15 17:49:43       10051              6 -36.728260   

    longitude dayweather  
0  174.714820    Clouds2  
1  174.741590    Clouds3  
2  174.714432    Clouds4  
3  174.723397    Clouds4  
4  174.715083        NaN  
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43696483

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档