Home > Software design >  Building individual classes, count and show in separate DF
Building individual classes, count and show in separate DF

Time:10-31

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?

CodePudding user response:

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.

  • Related