pandas:返回系列中每个字符串值的所有匹配键

时间:2018-02-05 03:59:00

标签: regex pandas series

如何将搜索列表中所有匹配的键作为逗号分隔值返回。

例如,

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

我想得到这个:

  

['猫,狗','猫','狗','猫','无']

2 个答案:

答案 0 :(得分:1)

只需public static void main(String[] args) { Flipper flipper = new Flipper(); flipper.drawCell(); flipper.run(); } ,然后执行split

str.contains

答案 1 :(得分:0)

我的同事帮助了我。 以下是我最终的表现:

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