0

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

1
  • 1
    Because the iterator makes a copy, which you are printing. It does not reflect changes to the dataframe at all Commented Feb 14, 2017 at 1:17

1 Answer 1

1

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

Reference: pandas.DataFrame.iterrows

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.