简体   繁体   中英

How to fill only missing values in one dataframe column with values from another dataframe in python?

I have 2 dataframes that have multiple columns in each of them. Both dataframes have the column "SendID" and "SendDateTime" as the only same columns. Both dataframes have "SendID" completely filled out. Df1 is missing 30,000 "SendDateTime". Df2 has all "SendID" and "SendDateTime" filled out. I want to get the missing "SendDateTime" in df1 from df2 by using the "SendID". Both dataframes have multiple columns that don't match up.

df1 (7 columns)

SendID   SendDateTime             Link
12345    10/12/2019  8:00:00 AM   Text Box
12345    10/12/2019  8:00:00 AM   View Browser
98765                             News
98765                             Social
45678    12/24/2019  11:00:00 AM  Shop
45678    12/24/2019  11:00:00 AM  Button

df2 (8 columns)

SendID   SendDateTime             Subject
12345    10/12/2019  8:00:00 AM   Hello
98765    11/19/2019  9:30:00 AM   Welcome
45678    12/24/2019  11:00:00 AM  Please Read

Desired output in df1 (same 7 columns) so all missing "SendDateTime" are filled in using "SendID" in df2:

SendID   SendDateTime             Link
12345    10/12/2019  8:00:00 AM   Text Box
12345    10/12/2019  8:00:00 AM   View Browser
98765    11/19/2019  9:30:00 AM   News
98765    11/19/2019  9:30:00 AM   Social
45678    12/24/2019  11:00:00 AM  Shop
45678    12/24/2019  11:00:00 AM  Button

I have tried:

miss = df1[df1['SendDateTime'].isnull()]
nonmiss = df1[df1['SendDateTime'].notnull()]
miss = miss.merge(df2, how='left', on='SendID')
df1 = nonmiss.append(miss)

I have also tried:

df1.update(df2)
print(df1)

How can I do this correctly?

You could get the distinct id/times from df2, then merge them on df1 after dropping the time column.

df2_t = df2[['SendID','SendDateTime']].drop_duplicates()
df1.drop(columns='SendDateTime').merge(df2_t, on='SendID')

Output

   SendID          Link             SendDateTime
0   12345      Text Box   10/12/2019  8:00:00 AM
1   12345  View Browser   10/12/2019  8:00:00 AM
2   98765          News   11/19/2019  9:30:00 AM
3   98765        Social   11/19/2019  9:30:00 AM
4   45678          Shop  12/24/2019  11:00:00 AM
5   45678        Button  12/24/2019  11:00:00 AM

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