Home > Net >  replace values in one dataframe with boolean conditions from another dataframe in python pandas
replace values in one dataframe with boolean conditions from another dataframe in python pandas

Time:01-05

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

CodePudding user response:

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
  • Related