10

I have a series of 633 values, ~50% of which are 0. Ideally, I'd like to bin my values (for choropleth mapping purposes) using qcut(), but that gives me an error due to non-unique bin edges. What's the best way to separate the data, quantile the non-zero values, and then recombine them into a single column such that zero values have a value of 0, and quantiled values have categorical.label + 1?

urschrei
  • 25,123
  • 12
  • 43
  • 84

1 Answers1

14

If you replace your zero values with NaN, cut() and qcut() behave as expected; those rows will have a bin value (from Categorical.labels) of -1:

df['density'].replace(to_replace=0, value=np.nan, inplace=True)
urschrei
  • 25,123
  • 12
  • 43
  • 84