【发布时间】:2020-01-07 08:18:40
【问题描述】:
我之前读过this 的文章,注意到pandas 的apply 函数、iterrows 和for 循环是处理pandas 数据帧的非常缓慢而高效的方式。
我正在对一些文本数据进行情感分析,但使用 apply 会导致高内存使用和低速,类似于 this 答案中所示。
%%time
data.merge(data.essay.apply(lambda s: pd.Series({'neg':sid.polarity_scores(s)['neg'],
'neu':sid.polarity_scores(s)['neu'],
'pos':sid.polarity_scores(s)['pos'],
'compound':sid.polarity_scores(s)['compound']})),
left_index=True, right_index=True)
如何使用内置的 numpy 或 pandas 函数来实现这一点? 编辑:- 该列包含论文文本数据
【问题讨论】:
-
你可以试试swifter
-
data.merge(data.essay.swifter.apply。像这样?
-
检查了一下,似乎它的性能甚至比 pandas apply 还要差,因为在我的情况下使用 swifter 的 pandas apply 但也适用于 sample,从而导致额外的开销。
标签: python pandas numpy machine-learning sentiment-analysis