简体   繁体   中英

Pandas merge single column dataframe with another dataframe of multiple columns

I have one dataframe_1 as

        date
0 2020-01-01
1 2020-01-02
2 2020-01-03
3 2020-01-04
4 2020-01-05

and another dataframe_2 as

             date source dest     price
634647 2020-09-18    EUR  USD  1.186317
634648 2020-09-19    EUR  USD  1.183970
634649 2020-09-20    EUR  USD  1.183970

I want to merge them on 'date' but the problem is dataframe_1 last date is '2021-02-15' and dataframe_2 last date is '2021-02-01'.

I want the resulting dataframe as

             date source dest     price
634647 2021-02-01    EUR  USD  1.186317
634648 2021-02-02    NaN  NaN  NaN
634649 2021-02-03    Nan  NaN  NaN
...
             date source dest     price
634647 2021-02-13    NaN  NaN  NaN
634648 2021-02-14    NaN  NaN  NaN
634649 2021-02-25    NaN  NaN  NaN

But I am not able to do it using pd.merge, please ignore the indices in the dataframes. Thanks a lot in advance.

you can use join to do it.

df1.set_index('date').join(df2.set_index('date'))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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