pandas - mask dataframe by column name

Fabio Lamanna

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.

KT.

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Changing a specific column name in pandas DataFrame

From Java

Select the row values of dataframe if row name is present in column name of another dataframe in pandas

From Dev

Pandas: Rename single DataFrame column without knowing column name

From Dev

Extract array (column name, data) from Pandas DataFrame

From Dev

pandas DataFrame set value on boolean mask

From Dev

Querying Pandas DataFrame with column name that contains a space or using the drop method with a column name that contains a space

From Dev

How to store the name of rows and column index in pandas DataFrame?

From Dev

How to replace a value in a pandas dataframe with column name based on a condition?

From Dev

pandas dataframe column name: remove special character

From Dev

Python/Pandas dataframe - return column name

From Dev

Return the column name(s) for a specific value in a pandas dataframe

From Dev

giving a name to a pandas dataframe?

From Dev

Combining Pandas Dataframe with indexed value as column name

From Dev

Pandas dataframe get all rows between zero(0) of mask column and get first and last row of each group

From Dev

Subsetting a dataframe in pandas according to column name values

From Dev

Mask cells where column name equals index (pandas)

From Dev

How to get column name for second largest row value in pandas DataFrame

From Dev

splitting a column into multiple columns with specific name in pandas dataframe

From Dev

Pandas dataframe column name not formatting as expected

From Dev

Pandas Dataframe Filtering Columns and return column name

From Dev

Python/Pandas dataframe - return column name

From Dev

Return the column name(s) for a specific value in a pandas dataframe

From Dev

giving a name to a pandas dataframe?

From Dev

Sum pandas dataframe column values based on condition of column name

From Dev

Name a column added with pandas dataframe

From Dev

Filtering a Pandas Dataframe with a boolean mask

From Dev

Convert pandas Series with categories as values to DataFrame mask

From Dev

python pandas - subsetting a dataframe with integer as column name

From Dev

How to merge pandas DataFrame with same column name?

Related Related

  1. 1

    Changing a specific column name in pandas DataFrame

  2. 2

    Select the row values of dataframe if row name is present in column name of another dataframe in pandas

  3. 3

    Pandas: Rename single DataFrame column without knowing column name

  4. 4

    Extract array (column name, data) from Pandas DataFrame

  5. 5

    pandas DataFrame set value on boolean mask

  6. 6

    Querying Pandas DataFrame with column name that contains a space or using the drop method with a column name that contains a space

  7. 7

    How to store the name of rows and column index in pandas DataFrame?

  8. 8

    How to replace a value in a pandas dataframe with column name based on a condition?

  9. 9

    pandas dataframe column name: remove special character

  10. 10

    Python/Pandas dataframe - return column name

  11. 11

    Return the column name(s) for a specific value in a pandas dataframe

  12. 12

    giving a name to a pandas dataframe?

  13. 13

    Combining Pandas Dataframe with indexed value as column name

  14. 14

    Pandas dataframe get all rows between zero(0) of mask column and get first and last row of each group

  15. 15

    Subsetting a dataframe in pandas according to column name values

  16. 16

    Mask cells where column name equals index (pandas)

  17. 17

    How to get column name for second largest row value in pandas DataFrame

  18. 18

    splitting a column into multiple columns with specific name in pandas dataframe

  19. 19

    Pandas dataframe column name not formatting as expected

  20. 20

    Pandas Dataframe Filtering Columns and return column name

  21. 21

    Python/Pandas dataframe - return column name

  22. 22

    Return the column name(s) for a specific value in a pandas dataframe

  23. 23

    giving a name to a pandas dataframe?

  24. 24

    Sum pandas dataframe column values based on condition of column name

  25. 25

    Name a column added with pandas dataframe

  26. 26

    Filtering a Pandas Dataframe with a boolean mask

  27. 27

    Convert pandas Series with categories as values to DataFrame mask

  28. 28

    python pandas - subsetting a dataframe with integer as column name

  29. 29

    How to merge pandas DataFrame with same column name?

HotTag

Archive