【发布时间】:2019-03-18 20:18:41
【问题描述】:
例如,数据框是:
df = pd.DataFrame(data = {'id': ['393848', '30495'],
'text' : ['This is Gabanna. @RT Her human Jose rushed past firefighters into his burning home to rescue her. She suffered burns on her nose and paws, but will be just fine. The family lost everything else. You can help them rebuild below. 14/10 for both (via @KUSINews)',
'Meet Milo. He’s a smiley boy who tore a ligament in his back left zoomer. The surgery to fix it went well, but he’s still at the hospital being monitored. He’s going to work very hard to fetch at full speed again, and you can help him do it below. 13/10']
})
我写了一些函数:
def tokenize(df):
def process_tokens(df): #return column with lists of tokens
def process_reg(text): #return plain text
return " ".join([i for i in re.sub(r'[^a-zA-Z\s]', "", str(text)).split()])
df['tokens'] = [process_reg(text).split() for text in df['text']]
return process_tokens(df)
tokenize(df)
def process(df): #return column with dicts
def process_group(token): #convert list of tokens into dictionery
return pd.DataFrame(token, columns=["term"]).groupby('term').size().to_dict()
df['dic'] = [process_group(token) for token in df['tokens']]
process(df)
他们一个接一个地工作得很好,我得到了预期:
我正在寻找将所有函数嵌套到一个中以便能够只传递一次数据帧的解决方案。
找不到。
请帮忙
【问题讨论】:
-
process(tokenize(df))? -
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-20-4af9fb28a669> in <module>() ----> 1 process(tokenize(df)) <ipython-input-15-ae9d3f8238ed> in process(df) 2 def process_group(token): #convert list of tokens into dictionery 3 return pd.DataFrame(token, columns=["term"]).groupby('term').size().to_dict() ----> 4 df['dic'] = [process_group(token) for token in df['tokens']] TypeError: 'NoneType' object is not subscriptable -
编写一个函数,调用
tokenize,然后调用process。 -
所以我还需要一个功能?你能举个例子吗?以及如何将它们全部嵌套到一个容器中?
-
嗯,在我看来,您想要一个您还没有的功能,因此您需要一个更多功能来获得您想要的功能。现在你不知道如何将
tokenize(df);process(df)放入函数中?
标签: python pandas function stringtokenizer