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
投诉,当我尝试投int
到one
:
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