Reputation: 12417
I would like to mask a df using another one regardless the name of the columns, but considering only their positions. For example, given these 2 df:
df = pd.DataFrame({'D': [10,20,0], 'E': [0, 30, 10], 'F': [0, 0, 10]})
df2 = pd.DataFrame({'A': [1,1,1], 'C': [1, 1, 1], 'E': [1, 1, 1]})
and using this mask
:
mask = (df != 0)
I would like:
A C E
0 NaN 1.0 1.0
1 NaN NaN 1.0
2 1.0 NaN NaN
Where D
masks A
, E
(of the first df) masks C
and F
masks E
(of the second df).
I can do this removing columns names:
df.columns = [''] * len(df.columns)
mask = (df != 0)
df2.columns = [''] * len(df2.columns)
dfn = df2.mask(mask)
But is there a better way? Thanks in advance
Upvotes: 1
Views: 68
Reputation: 862431
You are close, is possible convert boolean DataFrame to boolean 2d array:
mask = (df != 0).values
dfn = df2.mask(mask)
print (dfn)
A C E
0 NaN 1.0 1.0
1 NaN NaN 1.0
2 1.0 NaN NaN
Upvotes: 3