Skip to content
Advertisement

SettingWithCopyWarning, how to stop it?

I have this code, it’s working for the first 9 itterations and then I get the SettingWithCopyWarning and it doesnt continue on, what can I do?

JavaScript

So I have this now, but it still only runs through the first 10 lines of data! Something to do with the first for loop I think! (I know it’s still a for loop but its been requested as being in a for loop!)

JavaScript

Advertisement

Answer

You’re chaining your indexes (e.g., dataframe[col_index][row_index].

In general, you should use

  • dataframe.loc[row_index, col_index]
  • dataframe.iloc[row_index, col_index]
  • dataframe.at[row_index, col_index]
  • dataframe.iat[row_index, col_index]

But in your case, you don’t need any of that. In fact, you rarely need to loop through a dataframe.

In your case, I would do:

JavaScript

Or, you could be more clever about how your use your date objects:

JavaScript

And that gives me:

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement