简体   繁体   English

ValueError:bin 标签必须比 Pandas 'Cut()' 中的 bin 边数少一个

[英]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.

相关问题 将连续变量切割成类别(ValueError:Bin 标签必须比 bin 边数少一个) - Cutting continous variable into catagories ( ValueError: Bin labels must be one fewer than the number of bin edges) 将年龄转换为年龄范围组(ValueError:Bin 标签必须比 bin 边缘的数量少一) - convert ages to groups of age ranges getting (ValueError: Bin labels must be one fewer than the number of bin edges) Pandas qcut 错误:bin 标签必须比 bin 边缘的数量少一 - Pandas qcut error: Bin labels must be one fewer than the number of bin edges 通过 pd.qcut duplicates='drop' kwarg 后,“bin 标签必须比 bin 边缘的数量少一” - "Bin labels must be one fewer than the number of bin edges" after passing pd.qcut duplicates='drop' kwarg Qcut Pandas:ValueError:Bin边必须是唯一的 - Qcut Pandas : ValueError: Bin edges must be unique 为什么使用pandas qcut返回ValueError:Bin边必须是唯一的? - Why use pandas qcut return ValueError: Bin edges must be unique? Python ValueError:容器边缘必须唯一 - Python ValueError: Bin edges must be unique ValueError: `axis` 必须小于维数 (1) - ValueError: `axis` must be fewer than the number of dimensions (1) pd.qcut-ValueError:容器边缘必须唯一 - pd.qcut - ValueError: Bin edges must be unique 如何用非唯一的垃圾箱边缘切割熊猫系列? - How to cut Pandas Series with non-unique bin edges?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM