Reputation: 147
I'm trying to get data from df1 if it doesn't exist in df2 and col1 in df1 should be aligned with col3 in df2 ( same for col2 and col4)
Df1:
col1 col2
2 2
1 Nan
Nan 1
Df2:
col3 col4
Nan 1
1 Nan
Nan 1
Final_Df:
col1 col2
2 1
1 Nan
Nan 1
Upvotes: 2
Views: 141
Reputation: 30052
Just use pandas.DataFrame.update(other). The overwrite parameter explanation.
overwrite bool, default True
How to handle non-NA values for overlapping keys:
True: overwrite original DataFrame’s values with values from other.
False: only update values that are NA in the original DataFrame.
Note that df.update(other)
modifies in place using non-NA values from another DataFrame on matching column label.
df2.update(df1.set_axis(df2.columns, axis=1))
print(df2)
col3 col4
0 2 2
1 1 Nan
2 Nan 1
Upvotes: 2
Reputation: 14949
Make the column same / replace Nan with np.NAN / update the dataframe
df1.columns = df2.columns
df2 = df2.replace('Nan', np.NAN)
df2.update(df1, overwrite=False) # will only update the NAN values
Upvotes: 1