- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我想比较两列:Description
和 Employer
。我想查看是否在 Description
列中找到了来自 Employer
的关键字。我已将 Employer
列分解为单词并转换为列表。现在我想看看这些词是否在相应的 Description
列中。
示例输入:
print(df.head(25))
Date Description Amount AutoNumber \
0 3/17/2015 WW120 TFR?FR xxx8690 140.00 49246
2 3/13/2015 JX154 TFR?FR xxx8690 150.00 49246
5 3/6/2015 CANSEL SURVEY E PAY 1182.08 49246
9 3/2/2015 UE200 TFR?FR xxx8690 180.00 49246
10 2/27/2015 JH401 TFR?FR xxx8690 400.00 49246
11 2/27/2015 CANSEL SURVEY E PAY 555.62 49246
12 2/25/2015 HU204 TFR?FR xxx8690 200.00 49246
13 2/23/2015 UQ263 TFR?FR xxx8690 102.00 49246
14 2/23/2015 UT460 TFR?FR xxx8690 200.00 49246
15 2/20/2015 CANSEL SURVEY E PAY 1222.05 49246
17 2/17/2015 UO414 TFR?FR xxx8690 250.00 49246
19 2/11/2015 HI540 TFR?FR xxx8690 130.00 49246
20 2/11/2015 HQ010 TFR?FR xxx8690 177.00 49246
21 2/10/2015 WU455 TFR?FR xxx8690 200.00 49246
22 2/6/2015 JJ500 TFR?FR xxx8690 301.00 49246
23 2/6/2015 CANSEL SURVEY E PAY 1182.08 49246
24 2/5/2015 IR453 TFR?FR xxx8690 168.56 49246
26 2/2/2015 RQ574 TFR?FR xxx8690 500.00 49246
27 2/2/2015 UT022 TFR?FR xxx8690 850.00 49246
28 12/31/2014 HU521 TFR?FR xxx8690 950.17 49246
Employer
0 Cansel Survey Equipment
2 Cansel Survey Equipment
5 Cansel Survey Equipment
9 Cansel Survey Equipment
10 Cansel Survey Equipment
11 Cansel Survey Equipment
12 Cansel Survey Equipment
13 Cansel Survey Equipment
14 Cansel Survey Equipment
15 Cansel Survey Equipment
17 Cansel Survey Equipment
19 Cansel Survey Equipment
20 Cansel Survey Equipment
21 Cansel Survey Equipment
22 Cansel Survey Equipment
23 Cansel Survey Equipment
24 Cansel Survey Equipment
26 Cansel Survey Equipment
27 Cansel Survey Equipment
28 Cansel Survey Equipment
我试过类似的东西,但它似乎不起作用。:
df['Text_Search'] = df['Employer'].apply(lambda x: x.split(" "))
df['Match'] = np.where(df['Description'].str.contains("|".join(df['Text_Search'])), "Yes", "No")
我想要的输出如下所示:
Date Description Amount AutoNumber \
0 3/17/2015 WW120 TFR?FR xxx8690 140.00 49246
2 3/13/2015 JX154 TFR?FR xxx8690 150.00 49246
5 3/6/2015 CANSEL SURVEY E PAY 1182.08 49246
9 3/2/2015 UE200 TFR?FR xxx8690 180.00 49246
10 2/27/2015 JH401 TFR?FR xxx8690 400.00 49246
11 2/27/2015 CANSEL SURVEY E PAY 555.62 49246
12 2/25/2015 HU204 TFR?FR xxx8690 200.00 49246
13 2/23/2015 UQ263 TFR?FR xxx8690 102.00 49246
14 2/23/2015 UT460 TFR?FR xxx8690 200.00 49246
15 2/20/2015 CANSEL SURVEY E PAY 1222.05 49246
17 2/17/2015 UO414 TFR?FR xxx8690 250.00 49246
19 2/11/2015 HI540 TFR?FR xxx8690 130.00 49246
20 2/11/2015 HQ010 TFR?FR xxx8690 177.00 49246
21 2/10/2015 WU455 TFR?FR xxx8690 200.00 49246
22 2/6/2015 JJ500 TFR?FR xxx8690 301.00 49246
23 2/6/2015 CANSEL SURVEY E PAY 1182.08 49246
24 2/5/2015 IR453 TFR?FR xxx8690 168.56 49246
26 2/2/2015 RQ574 TFR?FR xxx8690 500.00 49246
27 2/2/2015 UT022 TFR?FR xxx8690 850.00 49246
28 12/31/2014 HU521 TFR?FR xxx8690 950.17 49246
29 12/30/2014 WZ553 TFR?FR xxx8690 200.00 49246
32 12/29/2014 JW173 TFR?FR xxx8690 300.00 49246
33 12/24/2014 CANSEL SURVEY E PAY 1219.21 49246
34 12/24/2014 CANSEL SURVEY E PAY 434.84 49246
36 12/23/2014 WT002 TFR?FR xxx8690 160.00 49246
Employer Text_Search Match
0 Cansel Survey Equipment [Cansel, Survey, Equipment] No
2 Cansel Survey Equipment [Cansel, Survey, Equipment] No
5 Cansel Survey Equipment [Cansel, Survey, Equipment] Yes
9 Cansel Survey Equipment [Cansel, Survey, Equipment] No
10 Cansel Survey Equipment [Cansel, Survey, Equipment] No
11 Cansel Survey Equipment [Cansel, Survey, Equipment] Yes
12 Cansel Survey Equipment [Cansel, Survey, Equipment] No
13 Cansel Survey Equipment [Cansel, Survey, Equipment] No
14 Cansel Survey Equipment [Cansel, Survey, Equipment] No
15 Cansel Survey Equipment [Cansel, Survey, Equipment] Yes
17 Cansel Survey Equipment [Cansel, Survey, Equipment] No
19 Cansel Survey Equipment [Cansel, Survey, Equipment] No
20 Cansel Survey Equipment [Cansel, Survey, Equipment] No
21 Cansel Survey Equipment [Cansel, Survey, Equipment] No
22 Cansel Survey Equipment [Cansel, Survey, Equipment] No
23 Cansel Survey Equipment [Cansel, Survey, Equipment] Yes
24 Cansel Survey Equipment [Cansel, Survey, Equipment] No
26 Cansel Survey Equipment [Cansel, Survey, Equipment] No
27 Cansel Survey Equipment [Cansel, Survey, Equipment] No
28 Cansel Survey Equipment [Cansel, Survey, Equipment] No
29 Cansel Survey Equipment [Cansel, Survey, Equipment] No
32 Cansel Survey Equipment [Cansel, Survey, Equipment] No
33 Cansel Survey Equipment [Cansel, Survey, Equipment] Yes
34 Cansel Survey Equipment [Cansel, Survey, Equipment] Yes
36 Cansel Survey Equipment [Cansel, Survey, Equipment] No
最佳答案
这是快速且节省内存的向量化解决方案,它使用 sklearn.feature_extraction.text.CountVectorizer方法:
from sklearn.feature_extraction.text import CountVectorizer
vect = CountVectorizer(min_df=1, lowercase=True)
X = vect.fit_transform(df['Employer'])
cols_emp = vect.get_feature_names()
X = vect.fit_transform(df['Description'])
cols_desc = vect.get_feature_names()
common_cols_idx = [i for i,col in enumerate(cols_desc) if col in cols_emp]
df['Match'] = (X.toarray()[:, common_cols_idx] == 1).any(1)
来源 DF:
In [259]: df
Out[259]:
Date Description Amount AutoNumber Employer
0 3/17/2015 WW120 TFR?FR xxx8690 140.00 49246 Cansel Survey Equipment
2 3/13/2015 JX154 TFR?FR xxx8690 150.00 49246 Cansel Survey Equipment
5 3/6/2015 CANSEL SURVEY E PAY 1182.08 49246 Cansel Survey Equipment
9 3/2/2015 UE200 TFR?FR xxx8690 180.00 49246 Cansel Survey Equipment
10 2/27/2015 JH401 TFR?FR xxx8690 400.00 49246 Cansel Survey Equipment
11 2/27/2015 CANSEL SURVEY E PAY 555.62 49246 Cansel Survey Equipment
12 2/25/2015 HU204 TFR?FR xxx8690 200.00 49246 Cansel Survey Equipment
13 2/23/2015 UQ263 TFR?FR xxx8690 102.00 49246 Cansel Survey Equipment
14 2/23/2015 UT460 TFR?FR xxx8690 200.00 49246 Cansel Survey Equipment
15 2/20/2015 CANSEL SURVEY E PAY 1222.05 49246 Cansel Survey Equipment
17 2/17/2015 UO414 TFR?FR xxx8690 250.00 49246 Cansel Survey Equipment
19 2/11/2015 HI540 TFR?FR xxx8690 130.00 49246 Cansel Survey Equipment
20 2/11/2015 HQ010 TFR?FR xxx8690 177.00 49246 Cansel Survey Equipment
21 2/10/2015 WU455 TFR?FR xxx8690 200.00 49246 Cansel Survey Equipment
22 2/6/2015 JJ500 TFR?FR xxx8690 301.00 49246 Cansel Survey Equipment
23 2/6/2015 CANSEL SURVEY E PAY 1182.08 49246 Cansel Survey Equipment
24 2/5/2015 IR453 TFR?FR xxx8690 168.56 49246 Cansel IR453
26 2/2/2015 RQ574 TFR?FR xxx8690 500.00 49246 Cansel Survey Equipment
27 2/2/2015 UT022 TFR?FR xxx8690 850.00 49246 Cansel Survey Equipment
28 12/31/2014 HU521 TFR?FR xxx8690 950.17 49246 Cansel Survey HU521
结果:
In [261]: df
Out[261]:
Date Description Amount AutoNumber Employer Match
0 3/17/2015 WW120 TFR?FR xxx8690 140.00 49246 Cansel Survey Equipment False
2 3/13/2015 JX154 TFR?FR xxx8690 150.00 49246 Cansel Survey Equipment False
5 3/6/2015 CANSEL SURVEY E PAY 1182.08 49246 Cansel Survey Equipment True
9 3/2/2015 UE200 TFR?FR xxx8690 180.00 49246 Cansel Survey Equipment False
10 2/27/2015 JH401 TFR?FR xxx8690 400.00 49246 Cansel Survey Equipment False
11 2/27/2015 CANSEL SURVEY E PAY 555.62 49246 Cansel Survey Equipment True
12 2/25/2015 HU204 TFR?FR xxx8690 200.00 49246 Cansel Survey Equipment False
13 2/23/2015 UQ263 TFR?FR xxx8690 102.00 49246 Cansel Survey Equipment False
14 2/23/2015 UT460 TFR?FR xxx8690 200.00 49246 Cansel Survey Equipment False
15 2/20/2015 CANSEL SURVEY E PAY 1222.05 49246 Cansel Survey Equipment True
17 2/17/2015 UO414 TFR?FR xxx8690 250.00 49246 Cansel Survey Equipment False
19 2/11/2015 HI540 TFR?FR xxx8690 130.00 49246 Cansel Survey Equipment False
20 2/11/2015 HQ010 TFR?FR xxx8690 177.00 49246 Cansel Survey Equipment False
21 2/10/2015 WU455 TFR?FR xxx8690 200.00 49246 Cansel Survey Equipment False
22 2/6/2015 JJ500 TFR?FR xxx8690 301.00 49246 Cansel Survey Equipment False
23 2/6/2015 CANSEL SURVEY E PAY 1182.08 49246 Cansel Survey Equipment True
24 2/5/2015 IR453 TFR?FR xxx8690 168.56 49246 Cansel IR453 True
26 2/2/2015 RQ574 TFR?FR xxx8690 500.00 49246 Cansel Survey Equipment False
27 2/2/2015 UT022 TFR?FR xxx8690 850.00 49246 Cansel Survey Equipment False
28 12/31/2014 HU521 TFR?FR xxx8690 950.17 49246 Cansel Survey HU521 True
一些解释:
In [266]: cols_desc
Out[266]:
['cansel',
'fr',
'hi540',
'hq010',
'hu204',
'hu521',
'ir453',
'jh401',
'jj500',
'jx154',
'pay',
'rq574',
'survey',
'tfr',
'ue200',
'uo414',
'uq263',
'ut022',
'ut460',
'wu455',
'ww120',
'xxx8690']
In [267]: cols_emp
Out[267]: ['cansel', 'equipment', 'hu521', 'ir453', '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0, 1, 0, 1],
[0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1],
[0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1],
[0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1],
[0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1]], dtype=int64)
In [271]: X.toarray()[:, common_cols_idx]
Out[271]:
array([[0, 0, 0, 0],
[0, 0, 0, 0],
[1, 0, 0, 1],
[0, 0, 0, 0],
[0, 0, 0, 0],
[1, 0, 0, 1],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[1, 0, 0, 1],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[1, 0, 0, 1],
[0, 0, 1, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 1, 0, 0]], dtype=int64)
In [272]: X.toarray()[:, common_cols_idx] == 1
Out[272]:
array([[False, False, False, False],
[False, False, False, False],
[ True, False, False, True],
[False, False, False, False],
[False, False, False, False],
[ True, False, False, True],
[False, False, False, False],
[False, False, False, False],
[False, False, False, False],
[ True, False, False, True],
[False, False, False, False],
[False, False, False, False],
[False, False, False, False],
[False, False, False, False],
[False, False, False, False],
[ True, False, False, True],
[False, False, True, False],
[False, False, False, False],
[False, False, False, False],
[False, True, False, False]], dtype=bool)
In [273]: (X.toarray()[:, common_cols_idx] == 1).any(1)
Out[273]: array([False, False, True, False, False, True, False, False, False, True, False, False, False, False, False, True, True, Fals
e, False, True], dtype=bool)
关于 python / Pandas : How to Match List of Strings with a DataFrame column,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42683249/
我正在处理一组标记为 160 个组的 173k 点。我想通过合并最接近的(到 9 或 10 个组)来减少组/集群的数量。我搜索过 sklearn 或类似的库,但没有成功。 我猜它只是通过 knn 聚类
我有一个扁平数字列表,这些数字逻辑上以 3 为一组,其中每个三元组是 (number, __ignored, flag[0 or 1]),例如: [7,56,1, 8,0,0, 2,0,0, 6,1,
我正在使用 pipenv 来管理我的包。我想编写一个 python 脚本来调用另一个使用不同虚拟环境(VE)的 python 脚本。 如何运行使用 VE1 的 python 脚本 1 并调用另一个 p
假设我有一个文件 script.py 位于 path = "foo/bar/script.py"。我正在寻找一种在 Python 中通过函数 execute_script() 从我的主要 Python
这听起来像是谜语或笑话,但实际上我还没有找到这个问题的答案。 问题到底是什么? 我想运行 2 个脚本。在第一个脚本中,我调用另一个脚本,但我希望它们继续并行,而不是在两个单独的线程中。主要是我不希望第
我有一个带有 python 2.5.5 的软件。我想发送一个命令,该命令将在 python 2.7.5 中启动一个脚本,然后继续执行该脚本。 我试过用 #!python2.7.5 和http://re
我在 python 命令行(使用 python 2.7)中,并尝试运行 Python 脚本。我的操作系统是 Windows 7。我已将我的目录设置为包含我所有脚本的文件夹,使用: os.chdir("
剧透:部分解决(见最后)。 以下是使用 Python 嵌入的代码示例: #include int main(int argc, char** argv) { Py_SetPythonHome
假设我有以下列表,对应于及时的股票价格: prices = [1, 3, 7, 10, 9, 8, 5, 3, 6, 8, 12, 9, 6, 10, 13, 8, 4, 11] 我想确定以下总体上最
所以我试图在选择某个单选按钮时更改此框架的背景。 我的框架位于一个类中,并且单选按钮的功能位于该类之外。 (这样我就可以在所有其他框架上调用它们。) 问题是每当我选择单选按钮时都会出现以下错误: co
我正在尝试将字符串与 python 中的正则表达式进行比较,如下所示, #!/usr/bin/env python3 import re str1 = "Expecting property name
考虑以下原型(prototype) Boost.Python 模块,该模块从单独的 C++ 头文件中引入类“D”。 /* file: a/b.cpp */ BOOST_PYTHON_MODULE(c)
如何编写一个程序来“识别函数调用的行号?” python 检查模块提供了定位行号的选项,但是, def di(): return inspect.currentframe().f_back.f_l
我已经使用 macports 安装了 Python 2.7,并且由于我的 $PATH 变量,这就是我输入 $ python 时得到的变量。然而,virtualenv 默认使用 Python 2.6,除
我只想问如何加快 python 上的 re.search 速度。 我有一个很长的字符串行,长度为 176861(即带有一些符号的字母数字字符),我使用此函数测试了该行以进行研究: def getExe
list1= [u'%app%%General%%Council%', u'%people%', u'%people%%Regional%%Council%%Mandate%', u'%ppp%%Ge
这个问题在这里已经有了答案: Is it Pythonic to use list comprehensions for just side effects? (7 个答案) 关闭 4 个月前。 告
我想用 Python 将两个列表组合成一个列表,方法如下: a = [1,1,1,2,2,2,3,3,3,3] b= ["Sun", "is", "bright", "June","and" ,"Ju
我正在运行带有最新 Boost 发行版 (1.55.0) 的 Mac OS X 10.8.4 (Darwin 12.4.0)。我正在按照说明 here构建包含在我的发行版中的教程 Boost-Pyth
学习 Python,我正在尝试制作一个没有任何第 3 方库的网络抓取工具,这样过程对我来说并没有简化,而且我知道我在做什么。我浏览了一些在线资源,但所有这些都让我对某些事情感到困惑。 html 看起来
我是一名优秀的程序员,十分优秀!