'How to fix pd.cut with duplicate edges

I got an error when I put this code. For the first row rf['r_quartile'] is passed but for the second one, I got this error.

rf['r_quartile'] = pd.qcut(rf['recency'], 4, ['1','2','3','4'])
rf['f_quartile'] = pd.qcut(rf['frequency'], 4, ['1','2','3','4'])
rf.head()

Here is an error, how to fix this.

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-68-f4184e0eb655> in <module>()
      1 #Computing Quantile of RFM values
      2 rf['r_quartile'] = pd.qcut(rf['recency'], 4, ['1','2','3','4'])
----> 3 rf['f_quartile'] = pd.qcut(rf['frequency'], 4, ['1','2','3','4'])
      4 rf.head()

1 frames
/usr/local/lib/python3.7/dist-packages/pandas/core/reshape/tile.py in qcut(x, q, labels, retbins, precision, duplicates)
    377         include_lowest=True,
    378         dtype=dtype,
--> 379         duplicates=duplicates,
    380     )
    381 

/usr/local/lib/python3.7/dist-packages/pandas/core/reshape/tile.py in _bins_to_cuts(x, bins, right, labels, precision, include_lowest, dtype, duplicates, ordered)
    412         if duplicates == "raise":
    413             raise ValueError(
--> 414                 f"Bin edges must be unique: {repr(bins)}.\n"
    415                 f"You can drop duplicate edges by setting the 'duplicates' kwarg"
    416             )

ValueError: Bin edges must be unique: array([  1.,   1.,   2.,   4., 154.]).
You can drop duplicate edges by setting the 'duplicates' kwarg

Thnak you.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source