1

I have below two dataframes

df1 = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7,8,9]})
df2 = pd.DataFrame({'A': [True, False, True], 'B': [False, True, False]})

when I try to mask df1 using Boolean condition from df2 as below

df3 = df1.mask(df2)

I get output as

>>> df3
     A    B   C
0  NaN  4.0 NaN
1  2.0  NaN NaN
2  NaN  6.0 NaN

But I want to ignore column C since this column doesn't exist in df2 and also show values as original My desired output is as below

>>> df3
     A    B   C
0   NaN  4    7
1   2    NaN  8
2   NaN  6    9

1 Answer 1

2

reindex before masking:

out = df1.mask(df2.reindex_like(df1).fillna(False))

Or:

out = df1.mask(df2.reindex(columns=df1.columns, fill_value=False))

Output:

     A    B  C
0  NaN  4.0  7
1  2.0  NaN  8
2  NaN  6.0  9
Sign up to request clarification or add additional context in comments.

3 Comments

something i was looking for !..also is there way to show original values like 2 instead of 2.0 ?
You can cover to Int64 with .astype('Int64')
@mozway. Hello ! Happy new year :-) I'm just here temporary ! gg for you reputation !!! Awesome.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.