Reputation: 171
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
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