尝试在DataFrame警告的切片副本上设置值

时间:2016-08-15 18:43:25

标签: python pandas

在我更新到消息建议的.loc方法后,我不确定为什么还会收到警告?这是误报吗?

eG.loc[:,'wt']=eG.groupby(['date','BB'])['m'].transform(weightFunction)
  

正在尝试在DataFrame

的切片副本上设置值
See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
  if __name__ == '__main__':

1 个答案:

答案 0 :(得分:6)

我猜你的eG DF是另一个DF的副本......

这是一个小型演示:

In [69]: df = pd.DataFrame(np.random.randint(0, 5, (10, 3)),  columns=list('abc'))

In [70]: cp = df[df.a > 0]

In [71]: cp.loc[:, 'c'] = cp.groupby('a').b.transform('sum')
c:\envs\py35\lib\site-packages\pandas\core\indexing.py:549: 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
  self.obj[item_labels[indexer[info_axis]]] = value

解决方法:

In [72]: cp = df[df.a > 0].copy()

In [73]: cp.loc[:, 'c'] = cp.groupby('a').b.transform('sum')

如果您不需要原装DF,可以节省内存:

In [74]: df = df[df.a > 0]

In [75]: df.loc[:, 'c'] = df.groupby('a').b.transform('sum')