如何在python 3中删除此警告

问题描述 投票:0回答:1

尝试使用panda降低和删除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]

如何删除警告

python python-3.x pandas
1个回答
1
投票

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

df["col1"] = df["col1"].str.strip().str.lower()
© www.soinside.com 2019 - 2024. All rights reserved.