我有2个数据框,格式如下:
df_search
SEARCH
part1
anotherpart
onemorepart
df_all
FILE EXTENSION PATH
part1_1 .prt //server/folder1/part1_1
part1_2 .prt //server/folder2/part1_2
part1_2 .pdf //server/folder3/part1_2
part1_3 .prt //server/folder2/part1_3
anotherpart_1 .prt //server/folder1/anotherpart_1
anotherpart_2 .prt //server/folder3/anotherpart_2
anotherpart_3 .prt //server/folder2/anotherpart_3
anotherpart_3 .cgm //server/folder1/anotherpart_3
anotherpart_4 .prt //server/folder3/anotherpart_4
onemorepart_1 .prt //server/folder2/onemorepart_1
onemorepart_2 .prt //server/folder1/onemorepart_2
onemorepart_2 .dwg //server/folder2/onemorepart_2
onemorepart_3 .prt //server/folder1/onemorepart_3
onemorepart_4 .prt //server/folder1/onemorepart_4
完整的df_search有15,000个项目。df_all有550,000个项目。我正在尝试基于文件字符串中的搜索项目字符串来合并两个数据框。我想要的输出是这样的:
SEARCH FILE EXTENSION PATH
part1 part1_1 .prt //server/folder1/part1_1
part1 part1_2 .prt //server/folder2/part1_2
part1 part1_2 .pdf //server/folder3/part1_2
part1 part1_3 .prt //server/folder2/part1_3
anotherpart anotherpart_1 .prt //server/folder1/anotherpart_1
anotherpart anotherpart_2 .prt //server/folder3/anotherpart_2
anotherpart anotherpart_3 .prt //server/folder2/anotherpart_3
anotherpart anotherpart_3 .cgm //server/folder1/anotherpart_3
anotherpart anotherpart_4 .prt //server/folder3/anotherpart_4
onemorepart onemorepart_1 .prt //server/folder2/onemorepart_1
onemorepart onemorepart_2 .prt //server/folder1/onemorepart_2
onemorepart onemorepart_2 .dwg //server/folder2/onemorepart_2
onemorepart onemorepart_3 .prt //server/folder1/onemorepart_3
onemorepart onemorepart_4 .prt //server/folder1/onemorepart_4
简单的数据框合并不起作用,因为字符串永远不会完全匹配(它始终是子字符串)。我还根据其他关于stackoverflow的问题尝试了以下方法:
df_all[df_all.name.str.contains('|'.join(df_search.search))]
这给了我df_all中所有找到的项目的完整列表,但是我不知道哪个搜索字符串返回了哪个结果。
我设法使其与for循环一起使用,但是对我的数据集来说很慢(67分钟):
super_df = []
for search_item in df_search.search:
df_entire.loc[df_entire.file.str.contains(search_item), 'search'] = search_item
temp_df = df_entire[df_entire.file.str.contains(search_item)]
super_df = pd.concat(super_df, axis=0, ignore_index=True)
通过矢量化可以做到这一点以提高性能吗?
谢谢
使用str.extract
+
insert
:
pat = "|".join(df_search.SEARCH)
df_all.insert(0, 'SEARCH', df_all['FILE'].str.extract("(" + pat + ')', expand=False))
print (df_all)
SEARCH FILE EXTENSION PATH
0 part1 part1_1 .prt //server/folder1/part1_1
1 part1 part1_2 .prt //server/folder2/part1_2
2 part1 part1_2 .pdf //server/folder3/part1_2
3 part1 part1_3 .prt //server/folder2/part1_3
4 anotherpart anotherpart_1 .prt //server/folder1/anotherpart_1
5 anotherpart anotherpart_2 .prt //server/folder3/anotherpart_2
6 anotherpart anotherpart_3 .prt //server/folder2/anotherpart_3
7 anotherpart anotherpart_3 .cgm //server/folder1/anotherpart_3
8 anotherpart anotherpart_4 .prt //server/folder3/anotherpart_4
9 onemorepart onemorepart_1 .prt //server/folder2/onemorepart_1
10 onemorepart onemorepart_2 .prt //server/folder1/onemorepart_2
11 onemorepart onemorepart_2 .dwg //server/folder2/onemorepart_2
12 onemorepart onemorepart_3 .prt //server/folder1/onemorepart_3
13 onemorepart onemorepart_4 .prt //server/folder1/onemorepart_4
我想像下面这样拆分这个字符串: {“我喜欢一个”、“橘子”、“以及”、“苹果”、“但主要是”、“苹果”、“苹果”,“苹果”,“橘子”,“是最好的水果。”} 在上面,我已经拆分了基于水果橙和苹果的字符串,但这两个水果也是子字符串列表的一部分。
问题内容: 有没有一种简单的方法来测试Python字符串“ xxxxABCDyyyy”,以查看其中是否包含“ ABCD”? 问题答案: if “ABCD” in “xxxxABCDyyyy”: # whatever
null 有人能帮我弄清楚为什么上面的代码给出了错误的答案吗?
问题内容: 我曾经在JavaScript中这样做: Swift没有此功能,如何做类似的事情? 问题答案: 编辑/更新: Xcode 11•Swift 5.1或更高版本 用法: 不区分大小写的样本 正则表达式样本
问题内容: 为什么以下算法对我来说不停止?(str是我要搜索的字符串,findStr是我要寻找的字符串) 问题答案: 最后一行造成了问题。永远不会为-1,所以会有无限循环。可以通过将代码的最后一行移到if块中来解决此问题。
本文向大家介绍替换Java字符串中的子字符串,包括了替换Java字符串中的子字符串的使用技巧和注意事项,需要的朋友参考一下 假设以下是我们的字符串。 我们想将子字符串“ Dead”替换为“ Alive”。为此,让我们使用以下逻辑。在这里,我们使用了while循环,并在其中找到了要替换的子字符串的索引。这样,我们一个接一个地替换了整个子字符串。 以下是替换子字符串的完整示例。 示例 输出结果