Pandas: return all matched keys for each row value in a series

How to return all matched keys from the search list in the form of values ​​separated by commas.

For instance,

s = pd.Series(['cat dog','hat cat','dog','fog cat','pet'])
searchfor = ['cat', 'dog']

I want to get the following:

['cat, dog', 'cat', 'dog', 'cat', 'None']

-1
source share
2 answers

He’s just split1st, then doingstr.contains

s1=s.str.split(' ',expand=True).stack()

s1[s1.str.contains('|'.join(searchfor))].groupby(level=0).apply(' '.join).reindex(s.index)
Out[778]: 
0    cat dog
1        cat
2        dog
3        cat
4        NaN
dtype: object
+1
source

My colleague helped me with this. Here is how I finally did it:

s = pandas.Series(['cat dog','hat cat','dog','fog cat','pet'])
searchfor = ['cat', 'dog']

b = ['']*len(s)
for i in numpy.arange(0,len(s)):
    for j in numpy.arange(0,len(searchfor)):
        b[i] = b[i] + ', ' + searchfor[j] if searchfor[j] in s[i] and b[i]!= '' else (searchfor[j] if searchfor[j] in s[i] else b[i])
df = DataFrame({'s': s, 'searchfor': [numpy.nan if i=='' else i for i in b]})

Df

    s        searchfor
0   cat dog  cat, dog
1   hat cat  cat
2   dog      dog
3   fog cat  cat
4   pet      NaN
0
source

Source: https://habr.com/ru/post/1693175/


All Articles