user2105632
user2105632

Reputation: 781

Python Pandas Dataframe change cell value in loop not reflecting immediately

I have a dataframe df as follows:

Regiment FinalScore

nighthawks -1

dragoons -1

scouts -1

for index_p, row_p in df.iterrows():
  df.ix[index_p, 'finalScore'] += 1
  print(row_p['finalScore'])

print(df)

What I get is:

-1

-1

-1

Regiment FinalScore

nighthawks 0

dragoons 0

scouts 0

Why is it that while changing the value of 'FinalScore' in the loop I do not get the updated values printed? And why are the updated values only reflected after the loop?

thanks

Upvotes: 0

Views: 382

Answers (1)

Louisiana Hasek
Louisiana Hasek

Reputation: 445

The dataframe iterator returns a copy of the row, so you don't see your changes to the underlying row.

Reference: pandas.DataFrame.iterrows

Upvotes: 1

Related Questions