我想根据列名在数据框(带有文本)中进行过滤。 对于给定的列,如果某项包含该列的名称,则将其保留,否则将被删除。 给定行的内容相同。
考虑此数据框:
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
谢谢
此致
答案 0 :(得分:1)
您可以尝试以下方法:
Derived
输出:
df.where(df.apply(lambda x: x.str.contains(x.name))).bfill().head(1)