When using pandas to assign a value to Dataframe, a seemingly inexplicable warning message appears:
Settingwithcopywarning:a value is trying to being set on a copy of slice from a DataFrame
Try using. loc[row_indexer,col_indexer] = value instead
The main idea of this alarm message is, "Try to assign a copy on a slice of dataframe, use. loc[row_indexer,col_indexer] = value instead of the current assignment operation." The reason for this alarm is that the pandas cannot judge whether the original dataframe is sliced, whether the view or the copy is generated. If the slice produces a view, the assignment modifies the original dataframe, and if a copy is produced, the original dataframe is not modified.
This alarm is generally generated due to the use of a chain index (chained indexing) assignment, while using. Loc[row_indexer,col_indexer] produces a new dataframe, in some cases This alarm problem can be solved (PS: There are more than one case of settingwithcopywarning alarms!) )。 If you choose to ignore this alarm, then you'd better look at your assignment is not successful (although I actually used to view the many times, is successful, but does not mean that every time can be successful, so the proposal should not ignore this warning).
In summary, it should be possible to avoid using chained indexes to copy DataFrame. Detailed explanation of the cause of the alarm and the solution, you can refer to "returning a view versus a copy" under the "indexing and Selecting Data" module in the official Pandas document Http://pandas.pydat A.org/pandas-docs/stable/indexing.html#returning-a-view-versus-a-copy. can also refer to I see a good article written:https://www.dataquest.io/blog/settingwithcopywarning/(Original English), https://www.jianshu.com/p/ 72274ccb647a (Chinese translation version).
Pandas Warning: settingwithcopywarning