Python 使用copy()后设置WithCopyWarning
我有如下代码Python 使用copy()后设置WithCopyWarning,python,pandas,copy,Python,Pandas,Copy,我有如下代码 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')#, excl
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()
它给了我以下的信息
__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
我想拥有独立于df\u numeric
的df\u non\u numeric
根据其他帖子中给出的建议,我使用了df_numeric['class'].copy()
我怎样才能避免该消息?我认为您需要
复制
,因为是按列类型筛选的切片操作,请检查:
如果稍后修改df\u non\u numeric
中的值,您将发现修改不会传播回原始数据(df
),并且不会发出警告
df_numeric=df.select_dtypes(include='number').copy()
df_non_numeric=df.select_dtypes(exclude='number').copy()