在将qcut应用于数据序列时,我得到了下面提到的索引错误



嗨,有人能告诉我这里有什么问题吗?

#How to bin a numeric series to 10 groups of equal size?
ser = pd.Series(np.random.random(20))
s=[0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1]
label=['1st','2nd','3rd','4th','5th','6th','7th','8th','9th','10th']
k=pd.qcut(ser,q=s,labels=label)

IndexError:索引76超出了大小为20的轴0的界限

提供的解决方案在qcut方法中直接传递列表,而不是将其定义为变量

您有哪一个panda版本?1.3.5中,一切正常:

ser = pd.Series(np.random.random(10))
s=[0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1]
label=['1st','2nd','3rd','4th','5th','6th','7th','8th','9th','10th']
k=pd.qcut(ser,q=s,labels=label)

输出:

0      6th
1      3rd
2      3rd
3      5th
4      1st
5      1st
6     10th
7      9th
8      2nd
9      5th
10     8th
dtype: category
Categories (10, object): ['1st' < '2nd' < '3rd' < '4th' ... '7th' < '8th' < '9th' < '10th']

最新更新