【发布时间】:2015-07-31 16:44:51
【问题描述】:
在下面的代码中,我将 DataFrame 的点按 X 值分组到 bins 中。现在我想为 Y 列分配一个组 ID,但是 pandas 不断向我抛出 SettingWithCopyWarning 类型的警告。我做错了什么?
import numpy as np
import pandas as pd
d = np.random.random((10, 2))
d[:, 1] = 0
m = pd.DataFrame(d, columns=("x", "gid"))
dx = 0.2
grp = m.groupby(lambda i: int(m["x"][i] / dx))
gid = 1
for name, group in grp:
group["gid"][:] = gid # This line crashes!
gid += 1
print(m)
这是抛出的警告:
/usr/lib/python3.4/site-packages/pandas/core/series.py:677: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame
See the the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
self._set_with(key, value)
sys:1: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame
See the the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
【问题讨论】:
-
查看stackoverflow.com/a/16949498/1571826 以获得更优雅、更灵活的分箱方法。
-
你用的是什么版本的熊猫?我也在运行 python 3.4,当我完全按原样运行代码时没有收到任何警告。
-
在 python 3.4.3、pandas 0.16.1 和 numpy 1.9.2 中运行良好