首页 > 解决方案 > 将值列添加到 pandas DataFrame

问题描述

我正在做一个简单的情绪分析,并被困在我觉得很简单的事情上。我正在尝试添加具有一组值的新列,在此示例中为compound值。但是在 for 循环迭代之后,它为所有行添加相同的值,而不是为每次迭代添加一个值。这些compound值是 DataFrame 中的最后一列。应该有一个快速修复。谢谢!

for i, row in real.iterrows():
   real['compound'] = sid.polarity_scores(real['title'][i])['compound']


title   text    subject date                                                        compound
0   As U.S. budget fight looms, Republicans flip t...   WASHINGTON (Reuters) - The head of a conservat...   politicsNews    December 31, 2017   0.2263
1   U.S. military to accept transgender recruits o...   WASHINGTON (Reuters) - Transgender people will...   politicsNews    December 29, 2017   0.2263
2   Senior U.S. Republican senator: 'Let Mr. Muell...   WASHINGTON (Reuters) - The special counsel inv...   politicsNews    December 31, 2017   0.2263
3   FBI Russia probe helped by Australian diplomat...   WASHINGTON (Reuters) - Trump campaign adviser ...   politicsNews    December 30, 2017   0.2263
4   Trump wants Postal Service to charge 'much mor...   SEATTLE/WASHINGTON (Reuters) - President Donal...   politicsNews    December 29, 2017   0.2263

在此处输入图像描述

标签: pythonpandasnlpsentiment-analysis

解决方案


国际大学联盟:

real['compound'] = real.apply(lambda row: sid.polarity_scores(row['title'])['compound'], axis=1)

推荐阅读