在系列上应用功能

时间:2018-06-14 14:16:02

标签: python module

您好,我的数据框系列看起来像这样

import re

keywords = {'file', 'filing', 'legal'}
max_words_after  = 10

key_re = re.compile(fr"""
(?:{'|'.join([w for w in keywords])})   #keyword options group
\s((?:[\s]?[A-Za-z\']+[\s]?)    #capture word. include with line-breaks
{{1,{max_words_after}}})                #1 to max_words_after
""", re.VERBOSE|re.IGNORECASE
)
data['Result'] = pd.Series
def word_search(series):
new_list = []
for f in series:
    temp = key_re.findall(f)
    new_list.append(temp)
return new_list
data["Result"] = data["Notes"].apply(word_search)

我想要创建一个新列,它会吐出下面函数的结果,找到该单词,然后吐出x个单词。

 TypeError                                 Traceback (most recent call last)
 <ipython-input-51-4122f6660074> in <module>()
 17         new_list.append(temp)
 18     return new_list
 ---> 19 data["Result"] = data["Notes"].apply(word_search)
 20 

 ~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\series.py 
 in apply(self, func, convert_dtype, args, **kwds)
 2549             else:
 2550                 values = self.asobject
-> 2551                 mapped = lib.map_infer(values, f, 
 convert=convert_dtype)
  2552 
  2553         if len(mapped) and isinstance(mapped[0], Series):

 pandas/_libs/src/inference.pyx in pandas._libs.lib.map_infer()

 <ipython-input-51-4122f6660074> in word_search(series)
 13 def word_search(series):
 14     new_list = []
 ---> 15     for f in series:
 16         temp = key_re.findall(f)
 17         new_list.append(temp)

 TypeError: 'float' object is not iterable

但是,我有以下错误:

{{1}}

任何人都可以帮我将这个功能应用到我的系列中吗?谢谢!

0 个答案:

没有答案