2017-07-31 59 views
2

的下面是我的代码(仅小样号):熊猫:缓冲区有维数错误

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) 

是什么原因造成这个错误?

+0

我无法重现您的错误。 – DyZ

+0

这很奇怪。我正在使用蟒蛇64位。这有什么可以做的吗? – fossekall

+0

我使用熊猫的'0.18.1',64位。 – DyZ

回答

4

这对我来说似乎是一个错误。即使是以下产生一个错误

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 
+0

是的,现在它可以工作。我真的开始质疑我对熊猫的理解。 – fossekall

+0

我报告了[** bug **](https://github.com/pandas-dev/pandas/issues/17130)。 – piRSquared