2017-06-19 5 views
1

I Perzentil von zwei Spalten zu berechnen versuchen die Pandas mit qcut Methode wie unten:Pandas: qcut Fehler: Valueerror: Bin Kanten muss eindeutig sein:

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) 

Die Säule float_col_quantile funktioniert gut, aber die Spalte int_col_quantile hat den folgenden Fehler. Irgendeine Idee, was ich hier falsch gemacht habe? Und wie kann ich dieses Problem beheben? Vielen Dank!


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

Mögliche Duplikate von [Wie qcut mit nicht eindeutigen bin-Kanten?] (Https://stackoverflow.com/questions/20158597/how-to-qcut-with-non-unique-bin-edges) Es gibt einige Problemumgehungen dort und beachten Sie auch, dass die Aktualisierung Pandas auf Version 0.20 könnte Ihr Problem lösen – JohnE

Antwort

1

Das Problem ist pandas.qcut die Behälter wählt, so dass Sie die gleiche Anzahl von Datensatz in jedem Fach/Quantil, aber der gleiche Wert mehr Bins/quantiles in nicht fallen kann.

Here ist eine Liste von Lösungen.

Verwandte Themen