How can I groupby a DataFrame at the same time I count the values and put in different columns?

GRech

I have a DataFrame that looks like the one below

Index  Category  Class
 0        1        A
 1        1        A
 2        1        B
 3        2        A
 4        3        B
 5        3        B

And I would like to get an output data frame that groups by category and have one column for each of the classes with the counting of the occurrences of that class in each category, such as the one below

Index Category   A   B
 0      1        2   1
 1      2        1   0
 2      3        0   2

So far I've tried various combinations of the groupby and agg methods, but I still can't get what I want. I've also tried df.pivot_table(index='Category', columns='Class', aggfunc='count'), but that return a DataFrame without columns. Any ideas of what could work in this case?

Cameron Riddell

You can use aggfunc="size" to achieve your desired result:

>>> df.pivot_table(index='Category', columns='Class', aggfunc='size', fill_value=0)

Class     A  B
Category
1         2  1
2         1  0
3         0  2

Alternatively, you can use .groupby(...).size() to get the counts, and then unstack to reshape your data as well:

>>> df.groupby(["Category", "Class"]).size().unstack(fill_value=0)

Class     A  B
Category
1         2  1
2         1  0
3         0  2

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I check in a pandas dataframe for a different values in different columns at the same time?

how I can use count and groupby at the same time?

How can I find out same values with more than one condition and put values of the same column in different columns?

How can I count the amount of values in different columns in oracle plsql

How can I count the same values ​in the array and create a different array?

How can I subtract specific values with different observations in the same Dataframe

How can I count unique values by columns

How can I count the values in the same column

How can I write an excel formula to count the number of times values in two separate columns are the same on the same row

How to count values for columns in a groupby dataframe?

sql: How can I count(*) same values and put an additional flag to each row?

How can I fill a column based on a difference between the values of two different columns, using groupby?

How can I have the same index for two different columns where the columns do not have unqiue values?

How can I compare the row values of select columns with the same columns in another dataframe?

Pandas Dataframe: how can i compare values in two columns of a row are equal to the ones in the same columns of a subsequent row?

How can I transform a dataframe by putting different values of a column into different columns?

how can I get the total count of same name of different columns in one table in MySQL?

How do I remove rows in a Pandas dataframe that have the same values in different columns?

How can I compare different values with same ids of different dataframe pandas

Pandas - how can I groupby different columns to calculate averages for the groups?

How can I count values that can be grouped by another columns in R

How can I keep all the rows in my dataframe that have the same values in all columns?

How can I manipulate dataframe columns with different values from an external vector (with dplyr)

How can I print the "PUT" and "POST" method at the same time?

How do I Group By and Count a column with 2 values at the same time?

How do I rearrange values in different columns in an R dataframe

How can I count replicate values across columns?

I have a dataframe in which one columns contains day and time and I want to put each day and its time in different column

How can I join the same data for different columns?