Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Groupby one column and count another column with a condition?

I was wondering if it is possible to groupby one column while counting the values of another column that fulfill a condition. Because my dataset is a bit weird, I created a similar one:

import pandas as pd

raw_data = {'name': ['John', 'Paul', 'George', 'Emily', 'Jamie'], 
            'nationality': ['USA', 'USA', 'France', 'France', 'UK'],     
            'books': [0, 15, 0, 14, 40]}  
df = pd.DataFrame(raw_data, columns = ['name', 'nationality', 'books'])

Say, I want to groupby the nationality and count the number of people that don't have any books (books == 0) from that country.

I would therefore expect something like the following as output:

nationality
USA      1
France   1
UK       0

I tried most variations of groupby, using filter, agg but don't seem to get anything that works.

Thanks in advance, BBQuercus :)

like image 220
BBQuercus Avatar asked May 20 '19 16:05

BBQuercus


People also ask

How to perform groupby and count with some numerical condition?

#groupby team and count number of 'pos' equal to 'Gu' df_count = df.groupby('team') ['pos'].apply(lambda x: (x=='Gu').sum()).reset_index(name='count') #view results print(df_count) team count 0 A 1 1 B 2 We can use similar syntax to perform a groupby and count with some numerical condition.

How to use group by and order by in SQL count?

SQL COUNT ( ) with group by and order by. In this page, we are going to discuss the usage of GROUP BY and ORDER BY along with the SQL COUNT() function. The GROUP BY makes the result set in summary rows by the value of one or more columns. Each same value on the specific column will be treated as an individual group. The utility...

How to count occurrences in column in pandas groupby?

Pandas GroupBy – Count occurrences in column 1 Import module 2 Create or import data frame 3 Apply groupby 4 Use any of the two methods 5 Display result More ...

When to use MySQL count () with group by?

SQL COUNT() with GROUP by. Last update on March 15 2019 07:13:36 (UTC/GMT +8 hours) The use of COUNT() function in conjunction with GROUP BY is useful for characterizing our data under various groupings. A combination of same values (on a column) will be treated as an individual group.


1 Answers

IIUC:

df.books.eq(0).astype(int).groupby(df.nationality).sum()

nationality
France    1
UK        0
USA       1
Name: books, dtype: int64
like image 69
piRSquared Avatar answered Sep 26 '22 06:09

piRSquared