Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 355
Hendrik Wiese Avatar asked Sep 30 '15 14:09

Hendrik Wiese


People also ask

Does pandas mean ignore NaN?

pandas mean() Key PointsBy default ignore NaN values and performs mean on index axis.

Is NaN same as Na in pandas?

In pandas, a missing value (NA: not available) is mainly represented by nan (not a number). None is also considered a missing value.


2 Answers

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
like image 140
Colonel Beauvel Avatar answered Sep 20 '22 13:09

Colonel Beauvel


If you line the columns up, then fillna() will do this:

df2.columns = df1.column
df1.fillna(df2, inplace=True)
df1

    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
like image 41
iayork Avatar answered Sep 19 '22 13:09

iayork