简体   繁体   中英

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM