dkk
dkk

Reputation: 171

How to filter the contents of a dataframe based on the columns names

I want to filter in a dataframe (with text) based on the column name. For a given column, if an item contains the name of the column, then it is maintained, if not it is removed. The contents of a given row is the same.

Consider this dataframe :

dog                     cat                     monkey
The cat is beautiful    The cat is beautiful    The cat is beautiful
The dog is beautiful    The dog is beautiful    The dog is beautiful
The monkey is beautiful The monkey is beautiful The monkey is beautiful

and what is expected :

dog                     cat                     monkey
The dog is beautiful    The cat is beautiful    The monkey is beautiful

Thanks you,

Regards,

Upvotes: 1

Views: 53

Answers (1)

Scott Boston
Scott Boston

Reputation: 153460

You can try this:

df.where(df.apply(lambda x: x.str.contains(x.name))).bfill().head(1)

Output:

                    dog                   cat                   monkey
0  The dog is beautiful  The cat is beautiful  The monkey is beautiful

Upvotes: 1

Related Questions