大熊猫:qcut错误:ValueError异常:滨边必须是唯一的:

问题描述:

我试图用熊猫们喜欢qcut方法如下计算两列的位数:大熊猫:qcut错误:ValueError异常:滨边必须是唯一的:

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.]) 
+1

可能的重复[如何qcut与非独特的bin边?](https://stackoverflow.com/questions/20158597/how-to-qcut-with-non-unique-bin-edges)有一些变通办法并注意到将熊猫更新到版本0.20可能会解决您的问题 – JohnE

问题是pandas.qcut选择合并,使你有相同数量的每个斌/位数的记录,但相同的值不能在多个垃圾箱/位数下降。

Here是解决方案的列表。