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
?
In Python pandas binning by distance is achieved by means of the cut() function. We group values related to the column Cupcake into three groups: small, medium and big. In order to do it, we need to calculate the intervals within each group falls.
You can also use df. replace(np. nan,0) to replace all NaN values with zero. This replaces all columns of DataFrame with zero for Nan values.
Pandas cut() function is used to separate the array elements into different bins . The cut function is mainly used to perform statistical analysis on scalar data.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With