Python将一列的字符串匹配到另一列的子字符串 [英] Python match the string of one column to the substring of another column
问题描述
我需要Python代码,该代码接受x列中的文本并遍历y列,并在Y中的每个值中搜索子字符串值x.下面是我的示例.如果可能的话,我希望它在字典中打印匹配的值和名称,或者以某种方式将其转换为每个列都有一个值的Pandas数据框.我在这个方面还很陌生,总是会出错.我的代码和错误如下.
I need Python code that takes the text in column x and loops over column y and searches for the substring values x within each value in Y. My example is below. IF possible, I would like it to print the value of the match and the name in a dictionary or someway I convert it to a Pandas dataframe with a value for each column. I'm fairly new at this keep getting errors. My code and error is below.
matches=['cat','bat','fat']
names=['turtle','bigcats','hfat1']
for x in matches:
if name.str.contains(x) == 1:
print(name)
ValueError:系列的真值不明确.使用a.empty,a.bool(),a.item(),a.any()或a.all().
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
推荐答案
由于您将此问题标记为熊猫:
Since you tagged this question as pandas:
import pandas as pd
import numpy as np
matches=['cat','bat','fat']
names=['turtle','bigcats','hfat1']
df = pd.DataFrame({'Name':names,'Matches':matches})
print(df)
开始数据框:
Matches Name
0 cat turtle
1 bat bigcats
2 fat hfat1
对contains
和join
创建的正则表达式使用str
访问权限:
Use str
access with contains
and regex created by join
:
df.loc[df.Name.str.contains('|'.join(df.Matches)),'Name'].tolist()
输出:
['bigcats', 'hfat1']
这篇关于Python将一列的字符串匹配到另一列的子字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!