[英]ValueError: Bin labels must be one fewer than the number of bin edges in Pandas 'Cut()'
I'm using cut() in Pandas to categorize the values of a numerical column.我在 Pandas 中使用 cut() 对数字列的值进行分类。
df['bmi_bin']=pd.cut(df['Body Mass Index'],bins=4,labels=[0,1,2,3])
Output: Output:
0 2
1 2
2 1
3 2
4 1
..
683 1
690 0
694 1
696 0
699 1
I tried putting the same code in a loop:我尝试将相同的代码放入循环中:
num_of_bins=4
for i in range(0,num_of_bins):
df['bmi_bin']=pd.cut(df['Body Mass Index'],bins=num_of_bins, labels=[i])
This is giving me the following error:这给了我以下错误:
ValueError: Bin labels must be one fewer than the number of bin edges
Where did I go wrong?我go哪里错了? Doesn't labels[i] give label values [0,1,2,3] which are fewer than 4?
labels[i] 不会给出小于 4 的 label 个值 [0,1,2,3] 吗?
Your code is trying to split dataframe into diff bins with lable.您的代码试图将 dataframe 拆分为带有标签的差异箱。 But bins and lables count not matching.
但是垃圾箱和标签计数不匹配。 In for loop you are spliting dataframe into 0, 1,2,3 bins in different iteration but lable is only one at a time whether its 0, 1,2 or 3. If you want to use loop then below can be used but not sure what output you are expecting -
在 for 循环中,您在不同的迭代中将 dataframe 拆分为 0、1、2、3 个 bin,但标签一次只有一个,无论是 0、1、2 还是 3。如果您想使用循环,则可以使用下面的内容,但不能使用确定您期待什么 output -
num_of_bins=4
lt=[]
for i in range(0,num_of_bins):
lt.append(i)
df['bmi_bin']=pd.cut(df['Body Mass Index'],bins=i, labels=lt)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.