Starting from this simple dataframe df
:
col1,col2
1,3
2,1
3,8
I would like to apply a boolean mask
in function of the name of the column. I know that it is easy for values:
mask = df <= 1
df = df[mask]
which returns:
mask:
col1 col2
0 True False
1 False True
2 False False
df:
col1 col2
0 1 NaN
1 NaN 1
2 NaN NaN
as expected. Now I would like to obtain a boolean mask based on the column name, something like:
mask = df == df['col_1']
which should return:
mask
col1 col2
0 True False
1 True False
2 True False
EDIT:
This seems weird, but I need those kind of masks to later filtering by columns seaborn heatmaps.
As noted in the comments, situations where you would need to get a "mask" like that seem rare (and chances are, you not in one of them). Consequently, there is probably no nice "built-in" solution for them in Pandas.
None the less, you can achieve what you need, using a hack like the following, for example:
mask = (df == df) & (df.columns == 'col_1')
Update:. As noted in the comments, if your data frame contains nulls, the mask computed this way will always be False
at the corresponding locations. If this is a problem, the safer option is:
mask = ((df == df) | df.isnull()) & (df.columns == 'col_1')
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments