简体   繁体   English

熊猫在日期时间合并两个不同大小的数据框

[英]Pandas Merge Two Different sized Dataframe on Datetime

I have two dataframes that i want to merge on date column. 我有两个要在日期列上合并的数据框。

First dataframe holds datetimes: 第一个数据框保存日期时间:

    DateTime,Date,Hour

    2015-01-01 00:00:00 | 2015-01-01 | 00 
    2015-01-01 00:00:01 | 2015-01-01 | 01 

    ...

    2015-01-01 23:00:00 | 2015-01-01 | 23

Second one holds it daily basis: 第二个每天举行一次:

> Date,Col3
> 
> 2015-01-01 | daily something1
> 
> 2015-01-02 | daily something2

-- -

I want to merge on Date column so that 24 hours in a date will have same daily features taken by second dataframe. 我想在“日期”列上进行合并,以使日期中的24小时具有第二个数据框所采用的相同每日功能。

2015-01-01 00:00:00 | 2015-01-01 | 00 |  daily something1

2015-01-01 01:00:00 | 2015-01-01 | 01 |  daily something1

...

2015-01-02 00:00:00 | 2015-01-01 | 23|  daily something2

It can be done by writing some code, but can i do this with using join or merge? 可以通过编写一些代码来完成,但是我可以使用联接或合并来做到这一点吗? tried to do it with left,right join but couldnt done it. 试图用左,右连接做到这一点,但不能做到这一点。

Let's merge the following two dataframes in the manner you described. 让我们按照您描述的方式合并以下两个数据框。 I don't know if there's a nice oneliner to accomplish this, and I'd like to see one, but this method works. 我不知道是否有一个很好的oneliner可以完成此任务,我想看看一个,但是这种方法有效。

import pandas as pd

df = pd.DataFrame({'DATE': pd.date_range(start='2016-01-01 00:00:00',
                                         freq='12H', periods=10)})
df2 = pd.DataFrame({'DATE': pd.date_range(start='2016-01-01',
                                          freq='D', periods=5),
                    'VALUE': range(0,5)})

# extract the date from each column
df['DATE_DAY'] = df['DATE'].dt.date
# even though the df2 DATE column only shows the date, it's still in
# a different type (datetime64[ns]), so we have to convert it as well
df2['DATE_DAY'] = df2['DATE'].dt.date

tmp = df.merge(df2, on='DATE_DAY')
>>> tmp
               DATE_x     DATE_y    DATE_DAY  VALUE
0 2016-01-01 00:00:00 2016-01-01  2016-01-01      0
1 2016-01-01 12:00:00 2016-01-01  2016-01-01      0
2 2016-01-02 00:00:00 2016-01-02  2016-01-02      1
3 2016-01-02 12:00:00 2016-01-02  2016-01-02      1
4 2016-01-03 00:00:00 2016-01-03  2016-01-03      2
5 2016-01-03 12:00:00 2016-01-03  2016-01-03      2
6 2016-01-04 00:00:00 2016-01-04  2016-01-04      3
7 2016-01-04 12:00:00 2016-01-04  2016-01-04      3
8 2016-01-05 00:00:00 2016-01-05  2016-01-05      4
9 2016-01-05 12:00:00 2016-01-05  2016-01-05      4

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM