问题
How to return all the matched keys of from a search list as comma-separated values.
For example,
s = pd.Series(['cat dog','hat cat','dog','fog cat','pet'])
searchfor = ['cat', 'dog']
I want to get this:
['cat, dog', 'cat', 'dog', 'cat', 'None']
回答1:
Just split
it 1st then doing str.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
回答2:
My colleague helped me with it. 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
来源:https://stackoverflow.com/questions/48615699/pandas-return-all-matched-keys-for-each-strings-value-in-a-series