Pandas Dataframe Filtering Columns and return column name

J-H

How can i filter a Pandas DataFrame which looks like this:

Head   Cat1   Cat2   Cat3
"A"     0      0      1
"B"     1      0      1

So that it returns for every row the Category-Column-Names if not 0

For example

"A"   "Cat3"
"B"   "Cat1" "Cat3"

I have no idea how to solve this and i deeply appreciate every help

EdChum

You can filter the columns first to get the cols of interest and then call apply and use the boolean mask to mask the cols:

In [18]:
cat_cols = df.columns[df.columns.str.contains('Cat')]
df[cat_cols].apply(lambda row: ', '.join(cat_cols[row == 1]), axis=1)

Out[18]:
0          Cat3
1    Cat1, Cat3
dtype: object

Also possible to retrieve the columns by accessing the .index attribute:

df[cat_cols].apply(lambda row: ', '.join(row.index[row == 1]), axis=1)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

pandas dataframe: return column that is a compression of other columns

From Dev

Python/Pandas dataframe - return column name

From Dev

Python/Pandas dataframe - return column name

From Dev

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

From Dev

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

From Dev

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

From Dev

Filtering a pandas dataframe comparing two columns

From Java

Filtering Pandas DataFrame by value in a column's lists

From Dev

Pandas DataFrame filtering based on second column

From Dev

Filtering a column of lists of strings in a Pandas DataFrame

From Dev

Pandas DataFrame filtering based on second column

From Dev

How to plot specific rows and columns of pandas dataframe (based on name of row and name of column) in bar plot with error bars?

From Dev

pandas return columns in dataframe that are not in other dataframe

From Dev

pandas - mask dataframe by column name

From Dev

Name a column added with pandas dataframe

From Dev

Combine columns in a Pandas DataFrame to a column of lists in a DataFrame

From Java

Filtering Pandas Dataframe using OR statement over a list of columns

From Dev

Filtering string/float/integer values in pandas dataframe columns

From Dev

Filtering for Dataframe - columns and rows

From Dev

Python pandas: dataframe grouped by a column(such as, name), and get the value of some columns in each group

From Dev

Add columns to pandas dataframe containing max of each row, AND corresponding column name

From Dev

Reverse columns of dataframe based on the column name

From Dev

Select dataframe columns based on number in column name

From Dev

Reverse columns of dataframe based on the column name

From Dev

Return groupby columns as new dataframe in Python Pandas

From Java

How to show all of columns name on pandas dataframe?

From Dev

Pandas stacking DataFrame and concatenating name of columns with index

From Dev

Python Pandas merge samed name columns in a dataframe

From Dev

Create pandas dataframe manually without columns name

Related Related

  1. 1

    pandas dataframe: return column that is a compression of other columns

  2. 2

    Python/Pandas dataframe - return column name

  3. 3

    Python/Pandas dataframe - return column name

  4. 4

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

  5. 5

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

  6. 6

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

  7. 7

    Filtering a pandas dataframe comparing two columns

  8. 8

    Filtering Pandas DataFrame by value in a column's lists

  9. 9

    Pandas DataFrame filtering based on second column

  10. 10

    Filtering a column of lists of strings in a Pandas DataFrame

  11. 11

    Pandas DataFrame filtering based on second column

  12. 12

    How to plot specific rows and columns of pandas dataframe (based on name of row and name of column) in bar plot with error bars?

  13. 13

    pandas return columns in dataframe that are not in other dataframe

  14. 14

    pandas - mask dataframe by column name

  15. 15

    Name a column added with pandas dataframe

  16. 16

    Combine columns in a Pandas DataFrame to a column of lists in a DataFrame

  17. 17

    Filtering Pandas Dataframe using OR statement over a list of columns

  18. 18

    Filtering string/float/integer values in pandas dataframe columns

  19. 19

    Filtering for Dataframe - columns and rows

  20. 20

    Python pandas: dataframe grouped by a column(such as, name), and get the value of some columns in each group

  21. 21

    Add columns to pandas dataframe containing max of each row, AND corresponding column name

  22. 22

    Reverse columns of dataframe based on the column name

  23. 23

    Select dataframe columns based on number in column name

  24. 24

    Reverse columns of dataframe based on the column name

  25. 25

    Return groupby columns as new dataframe in Python Pandas

  26. 26

    How to show all of columns name on pandas dataframe?

  27. 27

    Pandas stacking DataFrame and concatenating name of columns with index

  28. 28

    Python Pandas merge samed name columns in a dataframe

  29. 29

    Create pandas dataframe manually without columns name

HotTag

Archive