首页 > 解决方案 > 查找哪些列包含/匹配另一列的智能/pythonic方法

问题描述

我的问题标题听起来有点神秘,所以我希望这个例子能说明问题。

我在“FindMe”列中有一个值,我想知道这是否在“Search1”或“Search2”的任何一个选项中。我的逻辑有效(尽管如果它同时出现在搜索 1 和 2 中,我知道我有问题)

import pandas as pd
import numpy as np
data = {"Search1":["one_two","two_ten", "five_ten"],
        "Search2":["three_four","one_four","two_twelve"],
        "FindMe":["three","one","nine"]}
df =pd.DataFrame(data)

df["Present1"] = df.apply(lambda x: str(x.FindMe) in str(x.Search1), axis =1)
df["Present2"] = df.apply(lambda x: str(x.FindMe) in str(x.Search2), axis =1)

df["Present"] = np.where(df.apply(lambda x: str(x.FindMe) in str(x.Search1), axis =1) ==1, 
df.Search1,
                np.where(df.apply(lambda x: str(x.FindMe) in str(x.Search2), axis =1) ==1, 
df.Search2,""))
print(df)

就像我说我的“Present”列正常工作,返回找到它的列的值。实际上,我需要检查的列要多得多,所以是的,我可以创建嵌套的 where's ,但这感觉应该有更好的解决方案。

有什么想法吗?
Ĵ

标签: pandasstringnested

解决方案


列表理解可以完成这项工作

df['Present'] = [[s for s in l if w in s] for l, w in 
                    zip(df.filter(like='Search').to_numpy(), df['FindMe'])]

    Search1     Search2 FindMe       Present
0   one_two  three_four  three  [three_four]
1   two_ten    one_four    one    [one_four]
2  five_ten  two_twelve   nine            []

推荐阅读