I want to update df1 as per updated df1, if df1 have nans replace with values if df have values matched with ID column on both data frames.
ID QD QP QE
101 4 6 4
102 5 8 5
103 7 6 6
104 8 3 5
105 4 2 5
If your ID
columns is sorted and these two columns are one-to-one correspondence, you can use
df1[df1.isnull()] = df.values
print(df1)
ID QD QP QE
0 101 4.0 6.0 4.0
1 102 5.0 8.0 5.0
2 103 7.0 6.0 6.0
3 104 8.0 3.0 5.0
4 105 4.0 2.0 5.0
If not, you'd better set the ID
column as index and choose one among fillna
method, combine_first
method and update
method to update column according to index.
df1 = df1.set_index('ID')
# fillna
df1 = df1.fillna(df.set_index('ID').set_axis(df1.columns, axis=1)).reset_index()
# combine_first, if df is bigger than your original df1,
# the additional rows and columns are added
df1 = df1.combine_first(df.set_index('ID').set_axis(df1.columns, axis=1)).reset_index()
# update method will modify data inplace,
# you need to do reset index in separate step
df1.update(df.set_index('ID').set_axis(df1.columns, axis=1))
df1.reset_index()
print(df1)
ID QD QP QE
0 101 4.0 6.0 4.0
1 102 5.0 8.0 5.0
2 103 7.0 6.0 6.0
3 104 8.0 3.0 5.0
4 105 4.0 2.0 5.0
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.