简体   繁体   中英

Populate NaN value in column A of data frame 1 with corresponding value from data frame 2 based on condition

I am new to pandas and am having a really difficult time with this. I have two data frames:

Df1
Address            Zip            Parcel Number
123 Main St        12345          1234-567-890
456 Broadway Ave   55555          NaN

Df2
Address            Zip            Parcel Number
456 Main St        66666          5555-555-555
456 Broadway Ave   55555          7777-777-777

What I would like to do is: If the address & zip code columns in df1 are equal to the address & zip code columns of df2, populate the NaN value in the parcel number column of df1 with the corresponding parcel number value from df2.

Any help would be appreciated!

This is called combine_first . Index your dataframes on ['Address', 'Zip'] first:

Df1.set_index(['Address', 'Zip'], inplace=True)
Df2.set_index(['Address', 'Zip'], inplace=True)
Df1.combine_first(Df2)

You can reset_index the resulting dataframe if you want to keep Address and Zip as columns

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