Interval show negative values

Viewed 41

I want to get the distribution of a column. The ranges are prices and the int is the number of sales within that range.

df1=df['column'].value_counts(bins=15, sort=False)

(-2,000.0000, 42,000.0000]
103
(42,000.0000, 83,000.0000]
880
(83,000.0000, 125,000.0000]
649
(125,000.0000, 167,000.0000]
390
(167,000.0000, 208,000.0000]
257
(208,000.0000, 250,000.0000]
211
(250,000.0000, 292,000.0000]
158
(292,000.0000, 333,000.0000]
105

I'm positive there are no values under zero within the df.column but for some reason in the example above the range starts at -2000. Is there a way to achieve the same result without negative numbers in my interval i.e with the min of the df.column as the left side of the first interval ?

1 Answers

It's a good question, I don't know why the starting bin is smaller than the min value in your column. I've been able to repeat your observation.

You can get more control over where the bins start and stop by passing a list of bin positions created by np.arange like this example

import pandas as pd
import numpy as np

np.random.seed(1)
df = pd.DataFrame({
    'column':np.random.randint(2,2000,20)
})

#make the bins yourself
num_bins = 15
min_val = df['column'].min()
max_val = df['column'].max()
bin_size = (max_val-min_val)//num_bins
bins = np.arange(min_val,max_val,bin_size)

df1=df['column'].value_counts(bins=bins, sort=False)
print(df1)
Related