Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use groupby in Pandas to count things in one column in comparison to another

Maybe groupby is the wrong approach. Seems like it should work but I'm not seeing it...

I want to group an event by it's outcome. Here is my DataFrame (df):

Status  Event
SUCCESS Run
SUCCESS Walk
SUCCESS Run
FAILED  Walk

Here is my desired result:

Event   SUCCESS FAILED
Run     2       1
Walk    0       1

I'm trying to make a grouped object but I can't figure out how to call it to display what I want.

grouped = df['Status'].groupby(df['Event'])
like image 813
sparrow Avatar asked Jul 09 '16 05:07

sparrow


People also ask

How do I count from one column to another in Groupby?

Use pandas DataFrame. groupby() to group the rows by column and use count() method to get the count for each group by ignoring None and Nan values. It works with non-floating type data as well.

How do you count after Groupby in pandas?

The most simple method for pandas groupby count is by using the in-built pandas method named size(). It returns a pandas series that possess the total number of row count for each group. The basic working of the size() method is the same as len() method and hence, it is not affected by NaN values in the dataset.

Can you use Groupby with multiple columns in pandas?

groupby() can take the list of columns to group by multiple columns and use the aggregate functions to apply single or multiple aggregations at the same time.

How do I get the difference between two columns in pandas?

Difference between rows or columns of a pandas DataFrame object is found using the diff() method. The axis parameter decides whether difference to be calculated is between rows or between columns. When the periods parameter assumes positive values, difference is found by subtracting the previous row from the next row.


1 Answers

try this:

 pd.crosstab(df.Event, df.Status)

Status  FAILED  SUCCESS
Event                  
Run          0        2
Walk         1        1


len("df.groupby('Event').Status.value_counts().unstack().fillna(0)")
61

len("df.pivot_table(index='Event', columns='Status', aggfunc=len, fill_value=0)")
74

len("pd.crosstab(df.Event, df.Status)")
32
like image 52
Merlin Avatar answered Sep 21 '22 12:09

Merlin