pandas: return all matched keys for each strings value in a series

我怕爱的太早我们不能终老 提交于 2019-12-11 14:18:51

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!