我想根据列名称过滤数据框(带有文本)。 对于给定的列,如果某个项目包含该列的名称,则保留该项目,如果不包含该项目,则将其删除。 给定行的内容是相同的。
考虑这个数据框:
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
以及预期:
dog cat monkey
The dog is beautiful The cat is beautiful The monkey is beautiful
谢谢你,
问候,
最佳答案
你可以试试这个:
df.where(df.apply(lambda x: x.str.contains(x.name))).bfill().head(1)
输出:
dog cat monkey
0 The dog is beautiful The cat is beautiful The monkey is beautiful
关于python - 如何根据列名称过滤数据框的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55924970/