Pandas Warning: settingwithcopywarning

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.