Python 在另一个系列中查找一个系列的匹配项,并使用正则表达式匹配打印数据帧的整行
如何从包含3列的数据帧中找到col1中的第一个系列中的匹配项?我需要能够使用正则表达式,因为我的系列包含*作为该字段中任何内容的占位符 我有一个由以下数据组成的熊猫系列:Python 在另一个系列中查找一个系列的匹配项,并使用正则表达式匹配打印数据帧的整行,python,regex,python-3.x,pandas,dataframe,Python,Regex,Python 3.x,Pandas,Dataframe,如何从包含3列的数据帧中找到col1中的第一个系列中的匹配项?我需要能够使用正则表达式,因为我的系列包含*作为该字段中任何内容的占位符 我有一个由以下数据组成的熊猫系列: col1 joe\creed\found\match matt\creed\*\not adam\creed\notfound\match col1 col2 col3 joe2\creed2\found\match 2 23 matt2\creed2\found2\n
col1
joe\creed\found\match
matt\creed\*\not
adam\creed\notfound\match
col1 col2 col3
joe2\creed2\found\match 2 23
matt2\creed2\found2\not 2 23
adam\creed\notfound\match 2 23
matt\creed\found\not 2 23
我有另一个数据框,数据如下:
col1
joe\creed\found\match
matt\creed\*\not
adam\creed\notfound\match
col1 col2 col3
joe2\creed2\found\match 2 23
matt2\creed2\found2\not 2 23
adam\creed\notfound\match 2 23
matt\creed\found\not 2 23
我尝试执行以下代码,但没有成功
for item in series:
print(df[df.col1.str.contains(item, regex=True)]
及
我的预期产出如下:
col1 col2 col3
adam\creed\notfound\match 2 23
matt\creed\found\not 2 23
您可以这样做: 数据:
In [163]: s
Out[163]:
0 joe\creed\found\match
1 matt\creed\*\not
2 adam\creed\notfound\match
Name: col1, dtype: object
In [164]: df
Out[164]:
col1 col2 col3
0 joe2\creed2\found\match 2 23
1 matt2\creed2\found2\not 2 23
2 adam\creed\notfound\match 2 23
3 matt\creed\found\not 2 23
import re
# replacing '*' --> '[^\\]*' (in the escaped string: '\\\*' --> '[^\\\\]*')
pat = s.apply(re.escape).str.replace(r'\\\*', r'[^\\\\]*').str.cat(sep='|')
# use the following line instead, if `s` is a DataFrame (not a Series):
#pat = s.col1.apply(re.escape).str.replace(r'\\\*', r'[^\\\\]*').str.cat(sep='|')
In [161]: df[df.col1.str.contains(pat)]
Out[161]:
col1 col2 col3
2 adam\creed\notfound\match 2 23
3 matt\creed\found\not 2 23
In [162]: pat
Out[162]: 'joe\\\\creed\\\\found\\\\match|matt\\\\creed\\\\[^\\\\]*\\\\not|adam\\\\creed\\\\notfound\\\\match'
解决方案:
In [163]: s
Out[163]:
0 joe\creed\found\match
1 matt\creed\*\not
2 adam\creed\notfound\match
Name: col1, dtype: object
In [164]: df
Out[164]:
col1 col2 col3
0 joe2\creed2\found\match 2 23
1 matt2\creed2\found2\not 2 23
2 adam\creed\notfound\match 2 23
3 matt\creed\found\not 2 23
import re
# replacing '*' --> '[^\\]*' (in the escaped string: '\\\*' --> '[^\\\\]*')
pat = s.apply(re.escape).str.replace(r'\\\*', r'[^\\\\]*').str.cat(sep='|')
# use the following line instead, if `s` is a DataFrame (not a Series):
#pat = s.col1.apply(re.escape).str.replace(r'\\\*', r'[^\\\\]*').str.cat(sep='|')
In [161]: df[df.col1.str.contains(pat)]
Out[161]:
col1 col2 col3
2 adam\creed\notfound\match 2 23
3 matt\creed\found\not 2 23
In [162]: pat
Out[162]: 'joe\\\\creed\\\\found\\\\match|matt\\\\creed\\\\[^\\\\]*\\\\not|adam\\\\creed\\\\notfound\\\\match'
主要的困难是正确地转义“搜索模式”系列中的所有特殊字符(如
\
)。您可能需要执行以下操作:pat=s.col1.apply(re.escape).str.replace(r'\\*',r'[^\\]*').str.cat(sep='|'),具体取决于结构(我不确定@johnnyb的原始df结构)@scienceisthenewblack,谢谢-这是一个很好的观点!我添加了这个选项作为一个注释…不太清楚为什么,但必须删除其中一个后座。但是这个解决方案非常有效,谢谢。pat=s.col1.apply(re.escape).str.replace(r'\\*',r'[^\\]*').str.cat(sep='.'