2017-06-19 72 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.