根据 pandas 中另一个数据帧的掩码值在数据帧中生成 NaN

Make NaN in a dataframe based on mask value of another dataframe in pandas

我有一个数据框

df1 = pd.DataFrame([["A",1,98,56,51], ["B",1,99,74,36], ["C",1,97,82,83],["B",1,96,31,90], ["C",1,45,92,12], ["A",1,67,33,55]], columns=["id","date","c1","c2","c3"])

我有另一个具有相同列名的数据框

df2 = pd.DataFrame([["A",1,False,False,True], ["B",1,False,False,True], ["C",1,False,False,False],["B",1,False,True,False], ["C",1,True,False,True], ["A",1,False,True,False]], columns=["id","date","c1","c2","c3"])

我想要一个dataframe df_out,在df2中值为True,在df1中替换为blank/nan,如果为False则保持df1中的值。

预期输出:

df_out = pd.DataFrame([["A",1,98,56, ""], ["B",1,99,74,""], ["C",1,97,82,83],["B",1,96,"",90], ["C",1,"",92,""], ["A",1,67,"",55]], columns=["id","date","c1","c2","c3"])

怎么做?

试试 where

l = ['c1','c2','c3']
df1[l] = df1[l].where(df2[l]==False,'')
df1
Out[199]: 
  id  date  c1  c2  c3
0  A     1  98  56    
1  B     1  99  74    
2  C     1  97  82  83
3  B     1  96      90
4  C     1      92    
5  A     1  67      55