首页 > 解决方案 > 如何在 python 3 中删除此警告

问题描述

尝试使用熊猫降低和剥离python 3中的一列,但收到警告-正确的方法是什么,因此不会出现此警告

df["col1"] = df[["col1"]].apply(lambda x: x.str.strip())
df["col1"] = df[["col1"]].apply(lambda x: x.str.lower())

警告

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
  self[k1] = value[k2]

如何删除警告

标签: pythonpython-3.xpandas

解决方案


要摆脱此警告,请将其应用于系列而不是数据框。使用df[["col1"]]正在创建一个新的数据框,然后您将其设置为列。如果您只是修改列就可以了。此外,我将两者链接在一起。

df["col1"] = df["col1"].str.strip().str.lower()

推荐阅读