【问题标题】:How do I efficiently loop over this dataframe and perform a function using inbuilt numpy or pandas?如何有效地循环此数据帧并使用内置的 numpy 或 pandas 执行功能?
【发布时间】: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


【解决方案1】:

我找到了一种使用pandarallel 更快地执行此功能的方法。

使用默认的 pandas 应用功能需要 9 分 24 秒,

但是通过使用 pandarallel,它在 1 分 7 秒内完成了操作(使用 16 个工人)。

【讨论】:

    猜你喜欢
    • 2018-09-16
    • 1970-01-01
    • 2022-01-01
    • 2021-04-22
    • 1970-01-01
    • 2018-04-11
    • 1970-01-01
    • 2023-02-25
    • 1970-01-01
    相关资源
    最近更新 更多