1

The following code creates quartile columns with bins:

for(a, c) in zip(colnames, cols):
     cstats[c] = pd.qcut(cstats[a], 4, precision = 1)

I understand that even with precision set to 1 decimal place, the leftmost bin adjusts to include the smallest value in the dataset. I've seen some GitHub discussions around this problem, but would love an easy way to adjust any bin with a left value stretching to multiple decimal places rather than one. Current results look like this:

                      2020_QRange
0      (15.700000000000001, 37.0]
1                    (37.0, 50.9]
2                   (67.6, 184.0]

etc. What are my options to adjust that monstrosity? For example, how would I just round that down to 15.7?

Setting precision = 2 corrects the error but the result is too granular for my needs.

  • 1
    The intervals are immutable, but you could extract the left and right bounds to separate series objects and modify them there with the pandas round() function. See [this](https://stackoverflow.com/a/53996040/9987623) answer for more info on that. – AlexK Apr 27 '22 at 22:56
  • Thanks Alex, I'll consider doing this. Just my feeling, but given that there is a precision setting, then all results should conform to that choice, and perhaps qcut could be revised to handle this consistently. – Joseph Shuffield Apr 28 '22 at 15:11
  • The problem I'm having with both solutions is that the dataframe column containing the interval is type 'object', so these solutions do not seem to be working. qcut does not appear to be generating type 'interval' – Joseph Shuffield May 05 '22 at 20:09
  • For debugging help, please provide a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – AlexK May 05 '22 at 21:21

0 Answers0