johnnyb
johnnyb

Reputation: 1815

finding matches of one series in another series and print entire row of dataframe with regex matching

How can I find matches from my first series that are found in col1 from the dataframe with 3 columns? I need to be able to use regex as well due to my series containing * as a placeholder for anything that is in that field.

I have a pandas series that consists of data like below:

col1
joe\creed\found\match
matt\creed\*\not
adam\creed\notfound\match

I have another dataframe with data like below:

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

I have attempted to do the following code with no success.

for item in series:
    print(df[df.col1.str.contains(item, regex=True)]

and

for item in series:
    print(df[df.col1.isin([str(item)])

My expected output is as follows:

col1                       col2 col3
adam\creed\notfound\match  2    23
matt\creed\found\not       2    23

Upvotes: 1

Views: 170

Answers (1)

MaxU - stand with Ukraine
MaxU - stand with Ukraine

Reputation: 210842

You can do it this way:

Data:

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

Solution:

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'

The main difficulty is to correctly escape all special characters (like \) in the "search pattern" series.

Upvotes: 2

Related Questions