SettingWithCopyWarning after using copy()

微笑、不失礼 提交于 2019-12-11 04:47:51

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/55204404/settingwithcopywarning-after-using-copy

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!