gpt4 book ai didi

python - Pandas :缓冲区的维数错误

转载 作者:太空宇宙 更新时间:2023-11-03 13:32:12 25 4
gpt4 key购买 nike

以下是我的代码(仅模拟数字):

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)

是什么导致了这个错误?

最佳答案

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

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

关于python - Pandas :缓冲区的维数错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45424753/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com