How do I avoid a SettingWithCopyWarning in pandas?

Data School · 47K views · 2016 · 13:30

Open on YouTube

Python, Ruby & scripting languagesData science & statisticstutorial

Why Stack Overflow recommends it

Stack Overflow users link it when answering questions about SettingWithCopyWarning, since it explains views versus copies and why ignoring the warning is a bad idea.

Recommended by 3 people across 3 different months, 2018 to 2022.

What it is

A 13-minute pandas tutorial from Data School (Kevin Markham) on the SettingWithCopyWarning. It walks through two common situations that trigger the warning, explains why pandas raises it, and shows how to fix the code using .loc and .copy() instead of silencing the warning.

What people said

Recommends LazyNearestNeigbour · 2018-04 · Stack Overflow · on “SettingWithCopyWarning using pandas apply”

Make sure that df is a copy of another data frame. In that case, you should write your code like df = df_test.copy() This makes sure df is a copy and not a view. Learn more about this warning from the below link [this video]

Recommends haneulkim · 2021-09 · Stack Overflow · on “Python DataFrame Issue with Warning”

Simply ignoring warning is not a good practice as mentioned by answers below. It happens when `df` points to another `a_df` therefore fixing `df` may affect `a_df`. You should watch [this video]

Recommends Andre Nevares · 2022-07 · Stack Overflow · on “'value trying to be set on copy' warning”

Basically. when you are using DOT NOTATION (like df.your_column) part of the code tries to GET something The other part is trying to SET something To avoid this, you can use the syntax with LOC. There is a youtube video How do I avoid a SettingWithCopyWarning in pandas? [this video] Maybe you can understand better what happens under the hood. Also, I think that @Norwegian Salmon has the correct answer Note: I checked if this post is a question that someone can suggest an youtube video. Check this post [LINK] from @Michael Perrotta . In this case, I think so... If someone disagrees, please let me know. I just want to help and respect all the community.