【问题标题】:Error when trying to create a bin column via cut尝试通过 cut 创建 bin 列时出错
【发布时间】:2020-12-14 17:54:46
【问题描述】:

我在尝试创建包含以下分箱值的列时遇到错误:

bins = [1, 10, 20, 21, 34]
labels = [1-10, 11-20, 21-34, 35]
df['binned'] = pd.cut(df['Number of Clinics'], bins=bins, labels=labels)

我收到错误:ValueError: Categorical categories must be unique

不确定我错过了什么?诊所数列只包含一个介于 1 和 100 之间的值,我想在 1-10、11-20、21-34 和 35+ 之间分箱,谢谢

【问题讨论】:

    标签: python python-3.x pandas data-science


    【解决方案1】:

    labels 更改为str 并更正您的bin

    bins = [1, 10, 20, 34, 100]
    labels = ['1-10', '11-20', '21-34', '35+']
    

    【讨论】:

    • 谢谢!有没有我可以用数字代替 100 来显示 34 +?还是 34 - 无限?
    • @Chris90 标签 = ['1-10', '11-20', '21-34', '34 - Inf'] ?
    • @Chris90 bins = [1, 10, 20, 34, np.Inf]
    猜你喜欢
    • 1970-01-01
    • 2018-11-14
    • 1970-01-01
    • 2023-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-28
    相关资源
    最近更新 更多