Pandas:缓冲区维度错误

3
以下是我的代码(仅为模拟数字):
import pandas as pd 
d = {'x' : [1,4,6,9],
     'y' : [1,4,6,8]}
df = pd.DataFrame(d)
ct = pd.concat([df.x,
                pd.cut(df.y, bins=2)], axis=1)
gp = ct.groupby('x').y.value_counts().unstack().fillna(0)
print(gp)
print(gp[gp.columns[0]])
gp[gp.columns[0]] = gp[gp.columns[0]]/10

print(gp) 的输出结果为:

y  (0.993, 4.5]  (4.5, 8.0]
x                          
1           1.0         0.0
4           1.0         0.0
6           0.0         1.0
9           0.0         1.0

print(gp[gp.columns[0]]) 的输出结果如下:

x
1    1.0
4    1.0
6    0.0
9    0.0
Name: (0.993, 4.5], dtype: float64

但是下面这行代码:

gp[gp.columns[0]] = gp[gp.columns[0]]/10

抛出此错误:

ValueError: Buffer has wrong number of dimensions (expected 1, got 0)

这个错误是由什么引起的?

2
我无法重现你的错误。 - DYZ
那很奇怪。我正在使用Anaconda 64位版本。这可能与此有关吗? - fossekall
我使用Pandas '0.18.1',64位。 - DYZ
我正在使用0.20.3版本。 - fossekall
1个回答

6
这对我来说似乎是一个错误。即使以下内容也会产生错误。
gp.loc[:, gp.columns[0]] /= 10
ValueError: Buffer has wrong number of dimensions (expected 1, got 0)
然而,如果您为 pd.cut 提供标签,就可以解决这个问题。
d = {'x' : [1,4,6,9],
     'y' : [1,4,6,8]}
df = pd.DataFrame(d)
ct = pd.concat([df.x,
                pd.cut(df.y, bins=2, labels=range(2))], axis=1)
gp = ct.groupby('x').y.value_counts().unstack(fill_value=0)

gp.loc[:, gp.columns[0]] /= 10

gp

y    0  1
x        
1  0.1  0
4  0.1  0
6  0.0  1
9  0.0  1

是的,现在它可以工作了。我真的开始质疑自己对Pandas的理解了。 - fossekall

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接