0

I have code as below.i get bunch of SettingWithCopyWarning messages. But if i check 3 dataframes after the last statement, only df_numeric['class'] has value 99. In other two dataframes class value remains unchanged. WHy? i thought that due to SettingWithCopyWarning, after the last statement class values in all dataframe would change

please explain what is going on

import pandas as pd
import numpy as np
data = [['Alex',10,5,0],['Bob',12,4,1],['Clarke',13,6,0],['brke',15,1,0]]
df = pd.DataFrame(data,columns=['Name','Age','weight','class'],dtype=float) 

df_numeric=df.select_dtypes(include='number')#, exclude=None)[source]
df_non_numeric=df.select_dtypes(exclude='number')

df_non_numeric['class']=df_numeric['class'].copy()

df_numeric['class']=99
user2543622
  • 5,760
  • 25
  • 91
  • 159
  • so you want `df['class']=df_numeric['class']` ?? also is it not same as your previous question? – anky Mar 17 '19 at 06:56
  • i want to understand what is going on. I thought that `df['class']=df_numeric['class']`, but it is not. why? what do `SettingWithCopyWarning` indicate? – user2543622 Mar 17 '19 at 06:58
  • May be you want to go through this: https://stackoverflow.com/questions/20625582/how-to-deal-with-settingwithcopywarning-in-pandas it is explained nicely with all examples – anky Mar 17 '19 at 07:00

1 Answers1

1

A quick answer here. It's the most common warning in pandas.

Just like Understanding SettingwithCopyWarning in pandas said, pandas informs you that your operation might not have worked as expected and that you should check the result to make sure you haven’t made a mistake.


The reason for SettingwithCopyWarning

SettingwithCopyWarning usually happens when you are trying to assign new values to a subset df2 of the original data frame df1

But the original dataframe df1 hasn't changed at all!

enter image description here [Picture borrows from Understanding SettingwithCopyWarning in pandas]

How to nail it down?

  1. I recommend you go through this post Understanding SettingwithCopyWarning in pandas with detailed explanation and example code
  2. Like @anky_91 said, you can go through the posts in your comment list

Hope you go through all the mess!

WY Hsu
  • 1,837
  • 2
  • 22
  • 33