1

I have code as below.

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()

it gives me below message

__main__:1: 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

i want to have df_non_numeric independent from df_numeric

i used df_numeric['class'].copy() based upon suggestions given in other posts.

How could i avoid the message?

user2543622
  • 5,760
  • 25
  • 91
  • 159

1 Answers1

2

I think you need copy because DataFrame.select_dtypes is slicing operation, filtering by types of column, check Question 3:

df_numeric=df.select_dtypes(include='number').copy()
df_non_numeric=df.select_dtypes(exclude='number').copy()

If you modify values in df_non_numeric later you will find that the modifications do not propagate back to the original data (df), and that Pandas does warning.

jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252