【问题标题】:Error when calculating new binned value column计算新的分箱值列时出错
【发布时间】:2020-09-08 09:10:31
【问题描述】:

当我尝试合并列值时出现错误,我的代码如下,

bins = [1, 10, 20, 34, np.inf]
labels = ['1-10', '10-19', '20-34', '34+']
df['binned'] = pd.cut(df['Location Count'], bins=bins, labels=labels, include_lowest=True)

其中位置计数列包含数值,例如 1.0 或 20.0,

当我的分箱列中的位置计数值为 20.0 时,这给了我一个分箱值,即 10-19,我在我的代码中做错了吗?谢谢

【问题讨论】:

    标签: python python-3.x pandas


    【解决方案1】:

    labels 只是 cut bin 的显示,所以这里的bins 是正确输出的关键

    bins = [1, 10, 20, 34, np.inf]
    labels = ['1-10', '10-19', '20-34', '34+']
    df['binned'] = pd.cut(df['Location Count'], bins=bins, labels=labels, include_lowest=True, right=False)
    

    第二个 bin 默认是 10-20,它是 (10, 20] 现在我们将 right=False 更改为 [10,20)

    【讨论】:

    • 知道了,我认为我的最低收入已经解决了这个问题?
    • @Chris90 include_lowest Whether the first interval should be left-inclusive or not. 仅限第一个区间
    猜你喜欢
    • 1970-01-01
    • 2015-12-27
    • 2014-02-05
    • 2016-03-31
    • 1970-01-01
    • 2022-06-16
    • 1970-01-01
    • 1970-01-01
    • 2018-09-08
    相关资源
    最近更新 更多