Dan
Dan

Reputation: 3

How to keep all values from a dataframe except where NaN is present in another dataframe?

I am new to Pandas and I am stuck at this specific problem where I have 2 DataFrames in Pandas, e.g.

>>> df1
      A   B
0     1   9
1     2   6
2     3   11
3     4   8

>>> df2
      A   B
0     Nan   0.05
1     Nan   0.05
2     0.16   Nan
3     0.16   Nan

What I am trying to achieve is to retain all values from df1 except where there is a NaN in df2 i.e.

>>> df3
      A     B
0     Nan   9
1     Nan   6
2     3   Nan
3     4   Nan

I am talking about dfs with 10,000 rows each so I can't do this manually. Also indices and columns are the exact same in each case. I also have no NaN values in df1. As far as I understand df.update() will either overwrite all values including NaN or update only those that are NaN.

Upvotes: 0

Views: 724

Answers (1)

Ch3steR
Ch3steR

Reputation: 20669

You can use boolean masking using DataFrame.notna.

# df2 = df2.astype(float) # This needed if your dtypes are not floats.
m = df2.notna()
df1[m]
     A    B
0  NaN  9.0
1  NaN  6.0
2  3.0  NaN
3  4.0  NaN

Upvotes: 1

Related Questions