pandas DataFrame Warning of SettingWithCopyWarning Resolution

  • 2021-07-26 07:55:02
  • OfStack

Just come into contact with python soon, programming is also a 3-legged cat, so there is no good usage habit for these commonly used tools. After all, the programming language is a donkey. So recently, when using pandas's DataFrame at work, I encountered the following alarm:

SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead
See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy

debug has been looking for a lot on the Internet for a long time, but it still hasn't been solved. It was adjusted for a long time in the alarm sentence, and later it was found that the main problem didn't appear in the alarm sentence.

Give an example to repeat this question:


import pandas as pd
A = pd.DataFrame([[1,2,3],[2,3,4],[3,4,5]], columns = ['a','b','c'])
B = A[['a', 'b']]
B['a'] = B['a'] + 1 # same result by using B.loc[:,'a'] = B.loc[:,'a']+ 1

Output:

A
Out[1]:
a b c
0 1 2 3
1 2 3 4
2 3 4 5

B
Out[2]:
a b
0 1 2
1 2 3
2 3 4

B
Out[3]:
a b
0 2 2
1 3 3
2 4 4

Let's talk about my feeling first: This alarm mainly means that your current operation on B may change another DataFrame A, so you should be careful. (Of course, the actual warning does not mean this, but "operate on copy of one slice of DataFrame" I don't feel any problem, so please ask the great gods to answer it.)

The alarm appears on line 4, but the main problem is on line 3: You should use the. loc method to get the new DataFrame instead of using the [] reference directly.


C = A.loc[:,['a','b']]
C['a'] = C['a']+1

So there will be no alarm.

Personally, it seems that using. loc is a replicative reference to the original DataFrame, while the reference of [] is a pointer reference, which is related to the assignment characteristics of python itself. However, I saw that the value of A was not changed when B was changed. In a word, I just know why, but I don't know why. I hope a great God can help me solve my doubts.


Related articles: