我如何有效地循环访问此数据框并使用内置的numpy或pandas执行功能?

问题描述 投票:0回答:1

[我较早读过this文章,并注意到熊猫应用函数,迭代和for循环是使用熊猫数据帧的极其缓慢而有效的方式。

我正在对某些文本数据进行情感分析,但是使用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函数实现此功能?编辑:-该列包含论文文本数据

python pandas numpy machine-learning sentiment-analysis
1个回答
0
投票

我发现了一种通过使用pandarallel更快地执行此功能的方法。

通过使用默认的熊猫应用功能,它花了9分24秒,

但是通过使用pandarallel,它只用了1分钟7秒就完成了操作(使用16名工人)。

© www.soinside.com 2019 - 2024. All rights reserved.