1

I'm doing a simple sentiment analysis and am stuck on something that I feel is very simple. I'm trying to add an new column with a set of values, in this example compound values. But after the for loop iterates it adds the same value for all the rows rather than a value for each iteration. The compound values are the last column in the DataFrame. There should be a quick fix. thanks!

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

enter image description here

AndronikMk
  • 151
  • 1
  • 10
  • `real['compound'] =` assigns the whole column. Did you mean `row['compound']`? – G. Anderson Apr 01 '20 at 23:25
  • It didn't work for me when I switched to `row['compound']`. I would like to add to a row after every iteration. My initial though was `.append` – AndronikMk Apr 01 '20 at 23:36
  • That loop is not very idiomatic, have you read the Pandas docs? – AMC Apr 02 '20 at 00:50
  • Does this answer your question? [Adding new column to existing DataFrame in Python pandas](https://stackoverflow.com/questions/12555323/adding-new-column-to-existing-dataframe-in-python-pandas) – tbhaxor Apr 02 '20 at 01:40

1 Answers1

2

IIUC:

real['compound'] = real.apply(lambda row: sid.polarity_scores(row['title'])['compound'], axis=1)
Bruno Mello
  • 4,448
  • 1
  • 9
  • 39