我是 Pandas 的新手,经常收到臭名昭著的 SettingWithCopyWarning。如果我有如下代码,我会收到警告:
import pandas as pd
import numpy as np
t1 = pd.DataFrame({'A':np.random.randn(8), 'B': np.random.randint(0,4,8)})
table = t1[t1['B'] < 3]
print(table)
table.sort(['B'], inplace=True)
print(table)
在这种情况下,它是否只是警告我在运行排序时原始数据框(即“t1”)不会被排序?
是否有更可接受的方式来执行此类操作(获取数据帧的一部分,然后在该部分上应用函数)?
请您参考如下方法:
为什么不复制切片呢?那么您将不会收到警告。
a = table.copy()
您可以在这篇文章的答案中阅读更多内容 How to deal with SettingWithCopyWarning in Pandas? .
