【发布时间】:2017-06-19 19:03:21
【问题描述】:
假设我有以下 pandas DataFrame:
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 数据类型。我认为以下方法可以正常工作,但是当我尝试将 int 转换为 one 时,我收到了 SettingWithCopyWarning 投诉:
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
【问题讨论】: