pandas - 查找哪些列包含/匹配另一列的智能/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 ,但这感觉应该有更好的解决方案。
有什么想法吗?
Ĵ
解决方案
列表理解可以完成这项工作
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 []
推荐阅读
- linux - 使用 UNIX 命令行工具将 JSON 拆分为多行
- c# - 窗口大小更改时如何处理窗口内的 UserControl
- amazon-web-services - 在 AWS 中,是整个 VPC 获得一个 DNS 服务器还是 VPC 中的每个子网都获得一个 DNS 服务器?
- mysql - MySQL w/通过单个交叉引用表进行多个关联
- asp.net - 例外:无法创建 ActiveX 组件
- c# - .NET Core - 使用具有专用身份验证 API 的多个身份验证方案
- visual-c++ - ShellExecuteEx() child 总是出现在 Parent 之上
- django - 如何在 django 模板中读取列表
- https - Chrome 允许同时对同一资源的并行请求过多
- c - 使用 inline-assembly 读取 ELF 二进制文件 .rodata 部分的内容