【问题标题】:Running a for loop or .apply with a pandas series使用熊猫系列运行 for 循环或 .apply
【发布时间】:2022-01-20 18:28:18
【问题描述】:

我正在尝试为我的 pandas 系列运行一个 for 循环或 .apply 使用 lambdas。代码如下:

df['sentiment_score'] = df.apply(lambda x: analyzer.polarity_scores(x['Filtered_text']), axis=1)

我想要实现的是对于df['Filtered_text'] 中的每个单词,通过列应用analyzer.polartiy_scores(x['Filtered_text'])

df['Filtered_text'] 中存储的内容示例:

[website, needs, convinient, terrible]
[filters, mobile, version, site]

因此,对于这些单词中的每一个,我希望将其应用于analyzer.polarity_scores

我也试过这个:

df['sentiment_score'] = df['Filtered_text'].apply(lambda x: analyzer.polarity_scores(x))

但我收到此错误:

AttributeError: 'list' object has no attribute 'encode'

还有这个:

df['sentiment_score'] = df['Filtered_text'].apply(lambda x: [ft for ft in x: analyzer.polarity_scores(x)])

谢谢

【问题讨论】:

    标签: python pandas nltk


    【解决方案1】:

    我会使用列表推导来解决这个问题:

    df['sentiment_score'] = df['Filtered_text'].apply(lambda x: [analyzer.polarity_scores(e) for e in x])
    

    【讨论】:

    • 谢谢!这就是我在上一个示例中试图实现的目标。
    猜你喜欢
    • 2022-07-18
    • 2021-11-10
    • 2018-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多