0

I am fairly new to Pandas, and am trying to use it to analyse a large dataset. I have read everything I can find about it, but just can't get it to work. I am trying to update values in a dataframe whilst iterating over it row by row, but the values are not being updated in the dataframe.

for index, row in df.iterrows():
    for j in data_column:
        this_value = some_value
        print(this_value) # prints some_value
        df.loc[index].at[j] = this_value
        print(df.loc[index].at[j]) # prints 0 (not updated)
halfer
  • 19,824
  • 17
  • 99
  • 186
mtomtom
  • 33
  • 5
  • 1
    Can you add some data sample and expected output? Because `iterrows` is by default really slow and maybe exist some vectorized alternative. – jezrael Mar 28 '19 at 08:17

1 Answers1

0

Use DataFrame.at:

df.at[index, j] = this_value

instead combination loc and at:

df.loc[index].at[j] = this_value

But iterrows is used only for some specific solutions, better is use some alternatives.

Does iterrows have performance issues?

jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • I have already tried that, and I get the same result. Tried every version I could find! If there's nothing obviously wrong with the code I posted, then I wonder if there is another mistake somewhere else (I posted a very simplified, shortened version of my code). This is driving me crazy! Just out of interest though, why would you choose df.at over df.loc? I don't completely understand the difference between what they do. – mtomtom Mar 28 '19 at 08:25
  • 1
    @mtomtom - Hard to know what is wrong, is necessary [minimal, complete, and verifiable example](http://stackoverflow.com/help/mcve) – jezrael Mar 28 '19 at 08:27