Why does one use of iloc() give a SettingWithCopyWarning, but the other doesn't?

后端 未结 3 605
隐瞒了意图╮
隐瞒了意图╮ 2021-01-14 10:09

Inside a method from a class i use this statement:

self.__datacontainer.iloc[-1][\'c\'] = value

Doing this i get a \"SettingWithCopyWarnin

3条回答
  •  情深已故
    2021-01-14 10:45

    Don't focus on the warning. The warning is just an indication, sometimes it doesn't even come up when you expect it should. Sometimes you will notice it occurs inconsistently. Instead, just avoid chained indexing or generally working with what could be a copy.

    You wish to index by row integer location and column label. That's an unnatural mix, given Pandas has functionality to index by integer positions or labels, but not both simultaneously.

    In this case, you can use use integer positional indexing for both rows and columns via a single iat call:

    df.iat[-1, df.columns.get_loc('C')] = 3
    

    Or, if your index labels are guaranteed to be unique, you can use at:

    df.at[df.index[-1], 'C'] = 3
    

提交回复
热议问题