Building individual classes, count and show in separate DF

Question:

I use a DataFrame looking like this:

Index   Type    Value
0       A       4
1       A       9
2       C       51
3       B       40
4       C       32
5       C       14

I want to classify the items from the DataFrame in the classes/ranges 0-1, 11-50, 51-100 and create a separate DataFrame which shows the amount of Type for each of those classes.

Class   A   B   C
0-10    2   0   0
11-50   0   1   2
51-100  0   0   1

Can anybody help?

Asked By: Aukru

||

Answers:

You can accomplish this in two lines:

df['class'] = pd.cut(df['Value'], [0, 10, 50, 100])
df.groupby('class')['Type'].value_counts().rename("count").reset_index().pivot('class', 'Type', 'count').fillna(0).astype(int)

The first line creates the classes that you’re after, and the second gives the counts. Your final result here is a pivot table (hence the usage of pivot). The result is:

Type       A  B  C
class
(0, 10]    2  0  0
(10, 50]   0  1  2
(50, 100]  0  0  1

Feel free to map class to the exact format you request, but the general idea is to use pd.cut, which assigns a value based on the interval a value belongs to.

Answered By: Kraigolas
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.