[英]How can I get rid of settingwithcopywarning pandas
我有一个 pandas dataframe 有一系列列,包括列“A”我想向它添加 3 个新列,这种方法有效:
for sample in samples:
df= df1.loc[df1['sample'] == sample, ]
df.reset_index(inplace=True, drop=True)
df["new"] = df["A"].diff()
df["new2"] = df["new"].diff()
df["new3"] = np.nan
但是,所有这些操作都会发出以下警告:
SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame.
在没有警告的情况下分配新列的正确方法是什么? 我看过How to deal with SettingWithCopyWarning in Pandas? 但我不明白问题是什么。
谢谢
正如@jezrael 所提到的。 每当您想复制/子集 dataframe 时,请在末尾使用 .copy() 以避免此类警告。 要了解为什么会发生这种情况,我会在这篇文章中推荐“cs95”的第二个答案: 如何处理 Pandas 中的 SettingWithCopyWarning?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.