2017-06-19 45 views
0

比方说,我有以下的熊猫数据框:熊猫:更改列数据类型产生警告

df = pd.DataFrame({'one': ['Baseline', 5, 6], 'two': [10, 10, 10]}) 
print(df) 
print(df.dtypes) 
# one object 
# two  int64 

我要收集所有的行,其中df.one != 'Baseline',然后转换为one列在这个新的数据帧到int数据类型。我认为下面会工作得很好,但我得到一个SettingWithCopyWarning投诉,当我尝试投intone

df_sub = df[df['one'] != 'Baseline'] 
df_sub['one'] = df_sub['one'].astype(int) 

script.py:15. 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_sub['one'] = df_sub['one'].astype(int) 

的代码似乎好工作(见下文),但我想知道如何避免这个警告(我应该使用不同的方法等)。我正在关注this question以更改特定列的数据类型。我也试过df_sub.loc[:, 'one'] = df_sub['one'].astype(int)df_sub.loc[:, 'one'] = df_sub.loc[:, 'one'].astype(int),我得到了同样的错误。

print(df_sub.dtypes) 
# one  int64 
# two  int64 

回答

3

为了避免该警告,让您的数据帧的副本

df_sub = df[df['one'] != 'Baseline'].copy() # A deep copy of your dataframe otherwise it'll point to an existing one in memory.