简体   繁体   English

在日期时间列上合并 pandas

[英]Merging pandas on datetime column

I have two dataframes like the following:我有两个数据框,如下所示:

date                B
2017-01-03 01:00    0.017
2017-01-04 02:55    0.024
2017-01-05 16:53    -0.01
2017-01-06 10:22    0.024



date                A
2017-01-03 01:00    0.018
2017-01-06 09:16    -0.02

I would need to transform then to a one dataframe like that:然后我需要像这样转换为一个 dataframe :

date                A       B
2017-01-03 01:00    0.018   0.017
2017-01-04 02:55            0.024
2017-01-05 16:53            -0.01
2017-01-06 09:16    -0.02
2017-01-06 10:22            0.024

How can I get it?我怎么才能得到它?

naming df2 as the dataframe that contains column A, and df1 as the dataframe that contains column Bdf2命名为包含 A 列的 dataframe,将df1命名为包含 B 列的 dataframe

Use DataFrame.merge :使用DataFrame.merge

df2.merge(df1,on='date',how='outer',sort=True)

Output: Output:

             date      A      B
0 2017-01-03 01:00:00  0.018  0.017
1 2017-01-04 02:55:00    NaN  0.024
2 2017-01-05 16:53:00    NaN -0.010
3 2017-01-06 09:16:00 -0.020    NaN
4 2017-01-06 10:22:00    NaN  0.024

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

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