简体   繁体   中英

Merging data frame with overlapping columns

I have following DataFrames:

    stores = [['AA', 12, 'Red'], ['BB', 13, 'Red'], ['BB', 14, 'Red'], ['BB', 15, 'Red']]
    visits = [['BB', 13, 'Green'], ['BB', 14, 'Blue']]

    stores_df = pd.DataFrame(data=stores, columns=['retailer', 'store', 'color'])
    stores_df.set_index(['retailer', 'store'], inplace=True)

    visits_df = pd.DataFrame(data=visits, columns=['retailer', 'store', 'color'])
    visits_df.set_index(['retailer', 'store'], inplace=True)

                color
retailer store       
BB       13     Green
         14      Blue

               color
retailer store      
AA       12      Red
BB       13      Red
         14      Red
         15      Red

How I can merge them in order to get following result:

               color
retailer store      
AA       12      Red
BB       13      Green
         14      Blue
         15      Red

You can use update :

In [41]: stores_df.update(visits_df)

In [42]: stores_df
Out[42]:
                color
retailer store
AA       12       Red
BB       13     Green
         14      Blue
         15       Red

You want to use combine_first

visits_df.combine_first(stores_df)

在此处输入图片说明

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