【问题标题】:Pandas: changing column datatype produces warningPandas:更改列数据类型会产生警告
【发布时间】: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

【问题讨论】:

    标签: python pandas indexing


    【解决方案1】:

    为了避免该警告,请复制您的数据框

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

    【讨论】:

      猜你喜欢
      • 2015-07-31
      • 1970-01-01
      • 2020-07-24
      • 1970-01-01
      • 2020-05-12
      • 2019-06-18
      • 1970-01-01
      • 2011-06-02
      • 2018-05-25
      相关资源
      最近更新 更多