Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binning with zero values in pandas

Tags:

python

pandas

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?

like image 843
urschrei Avatar asked Sep 20 '13 16:09

urschrei


People also ask

How do you binning a panda in Python?

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.

How do you replace zeros in pandas?

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.

What is PD cut in pandas?

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.


1 Answers

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)
like image 191
urschrei Avatar answered Oct 11 '22 08:10

urschrei