Search code examples
pythonpandasmerging-data

Pandas: merge two dataframes ignoring NaN


Assume I have the following two DataFrames:

  X    Y    Z
1 0.0  0.0  0.0
2 1.0  2.0  3.0
3 4.0  2.0  0.0
4 NaN  NaN  NaN
5 NaN  NaN  NaN
6 NaN  NaN  NaN
7 NaN  NaN  NaN
8 NaN  NaN  NaN

and

  X.2  Y.2  Z.2
1 NaN  NaN  NaN
2 NaN  NaN  NaN
3 NaN  NaN  NaN
4 NaN  NaN  NaN
5 NaN  NaN  NaN
6 9.0  3.0  6.0
7 7.0  4.0  3.0
8 3.0  6.0  8.0

I would like to fill the missing data in the first DataFrame with the values from the second. Result should look like this:

  X    Y    Z
1 0.0  0.0  0.0
2 1.0  2.0  3.0
3 4.0  2.0  0.0
4 NaN  NaN  NaN
5 NaN  NaN  NaN
6 9.0  3.0  6.0
7 7.0  4.0  3.0
8 3.0  6.0  8.0

If possible I'd like to avoid creating a new DataFrame but fill up the first DataFrame in place.

How do I do this?


Solution

  • You can proceed simply with update which fills up the first dataframe df1 based on the value of df2:

    df2.columns = df1.columns
    
    df1.update(df2)
    
    In [118]: df1
    Out[118]:
        X   Y   Z
    1   0   0   0
    2   1   2   3
    3   4   2   0
    4 NaN NaN NaN
    5 NaN NaN NaN
    6   9   3   6
    7   7   4   3
    8   3   6   8