2017-06-19 101 views
1

我試圖用熊貓們喜歡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

回答

1

問題是pandas.qcut選擇合併,使你有相同數量的每個斌/位數的記錄,但相同的值不能在多個垃圾箱/位數下降。

Here是解決方案的列表。

相關問題