Harsh Wardhan
Harsh Wardhan

Reputation: 2148

Frequency Table by range in pandas returns 1 or 0

My dataframe looks like this

Total
0
0.5
1
1.5
2
2.5
10
10.5
11
11.5
12
12.5
5
5.5
6
6.5
7
7.5
15
15.5
16
16.5
17
17.5

I'm trying to get a frequency table of the above data following this answer

graph = df['Total'].apply(pd.Series.value_counts, bins=[0, 10, 20, 30, 40, 50, 60, 70])

But I'm getting this result enter image description here

Why am I not getting the actual count? And why is the range so weird? Im using Python 3.8.5 and Pandas 1.1.2.

Upvotes: 1

Views: 585

Answers (2)

Max Pierini
Max Pierini

Reputation: 2249

You need to sum them up

df['Total'].apply(pd.Series.value_counts, bins=[0, 10, 20, 30, 40, 50, 60, 70]).sum()

output

(-0.001, 10.0]    21
(10.0, 20.0]      15
(20.0, 30.0]       0
(30.0, 40.0]       0
(40.0, 50.0]       0
(50.0, 60.0]       0
(60.0, 70.0]       0
dtype: int64

update

Or better

df['Total'].apply(
    pd.Series.value_counts, 
    bins=np.arange(0, 101, 10)
).sum()

output

(-0.001, 10.0]    21
(10.0, 20.0]      15
(20.0, 30.0]       0
(30.0, 40.0]       0
(40.0, 50.0]       0
(50.0, 60.0]       0
(60.0, 70.0]       0
(70.0, 80.0]       0
(80.0, 90.0]       0
(90.0, 100.0]      0
dtype: int64

Upvotes: 1

piterbarg
piterbarg

Reputation: 8219

What is your desired output? Eg is this what you are after:

df.groupby(pd.cut(df['Total'], bins=[0, 10, 20, 30, 40, 50, 60, 70])).count()

output


            Total
Total   
(0, 10]     12
(10, 20]    11
(20, 30]    0
(30, 40]    0
(40, 50]    0
(50, 60]    0
(60, 70]    0

Upvotes: 1

Related Questions