运行 for 循环或 .apply 与 pandas 系列

Running a for loop or .apply with a pandas series

我正在尝试 运行 一个 for 循环或 .apply 对我的 pandas 系列使用 lambda。这是代码:

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)])

谢谢

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

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