【问题标题】:pandas: qcut error: ValueError: Bin edges must be unique:熊猫:qcut 错误:ValueError:Bin 边缘必须是唯一的:
【发布时间】:2017-11-22 04:17:52
【问题描述】:

我正在尝试使用 pandas qcut 方法计算两列的百分位数,如下所示:

my_df['float_col_quantile'] = pd.qcut(my_df['float_col'], 100, labels=False)
my_df['int_col_quantile'] = pd.qcut(my_df['int_col'].astype(float), 100, labels=False)

float_col_quantile 列工作正常,但int_col_quantile 列出现以下错误。知道我在这里做错了什么吗?我该如何解决这个问题?谢谢!


ValueError                                Traceback (most recent call last)
<ipython-input-19-b955e0b00953> in <module>()
      1 my_df['float_col_quantile'] = pd.qcut(my_df['float_col'], 100, labels=False)
----> 2 my_df['int_col_quantile'] = pd.qcut(my_df['int_col'].astype(float), 100, labels=False)


/usr/local/lib/python3.4/dist-packages/pandas/tools/tile.py in qcut(x, q, labels, retbins, precision)
    173     bins = algos.quantile(x, quantiles)
    174     return _bins_to_cuts(x, bins, labels=labels, retbins=retbins,
--> 175                          precision=precision, include_lowest=True)
    176 
    177 

/usr/local/lib/python3.4/dist-packages/pandas/tools/tile.py in _bins_to_cuts(x, bins, right, labels, retbins, precision, name, include_lowest)
    192 
    193     if len(algos.unique(bins)) < len(bins):
--> 194         raise ValueError('Bin edges must be unique: %s' % repr(bins))
    195 
    196     if include_lowest:

ValueError: Bin edges must be unique: array([  1.,   1.,   1.,   1.,   1.,   1.,   1.,   1.,   1.,   1.,   1.,
         1.,   1.,   1.,   1.,   1.,   1.,   1.,   1.,   1.,   1.,   1.,
         1.,   1.,   1.,   1.,   2.,   2.,   2.,   2.,   2.,   2.,   2.,
         2.,   2.,   2.,   2.,   2.,   2.,   2.,   2.,   2.,   2.,   2.,
         2.,   2.,   2.,   2.,   2.,   2.,   2.,   2.,   2.,   2.,   2.,
         2.,   2.,   2.,   2.,   2.,   2.,   2.,   2.,   2.,   2.,   2.,
         2.,   2.,   2.,   4.,   4.,   4.,   4.,   4.,   4.,   4.,   4.,
         4.,   4.,   4.,   4.,   4.,   4.,   4.,   4.,   4.,   4.,   4.,
         4.,   4.,   4.,   4.,   4.,   4.,   4.,   4.,   8.,   8.,   8.,
         8.,  10.])

【问题讨论】:

标签: python-3.x pandas dataframe quantile


【解决方案1】:

问题是 pandas.qcut 选择 bin 以便您在每个 bin/quantile 中拥有相同数量的记录,但相同的值不能落在多个 bin/quantiles 中。

Here 是解决方案列表。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-07-26
    • 2016-11-13
    • 2016-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-08
    • 1970-01-01
    相关资源
    最近更新 更多