在下面的一段代碼中,我將DataFrame的點按它們的X值分組到bin。現在我想給Y列分配一個組ID,但大熊貓不斷給我發出類型爲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
看一看http://stackoverflow.com/a/16949498/1571826了更優雅靈活的分檔方法。 –
你使用的是什麼版本的熊貓?我也在運行python 3.4,並且我完全按照原樣運行代碼時沒有收到任何警告。 – KCzar
在python 3.4.3,pandas 0.16.1和numpy 1.9.2中工作正常 – EdChum