Victor
Victor

Reputation: 5

How to groupby and count values in a specific column

I have a dataframe procs, where each month several ids took place, and also each id took place several times:

procs.groupby(['month', 'id']).size()

month               id                   
2015-02             UA-2015-02-06-000018-L1       5
                    UA-2015-02-09-000009-L1      10
                    UA-2015-02-10-000001-L1       5
                    UA-2015-02-10-000010-L1       3
                    UA-2015-02-16-000002-L1       6
                    UA-2015-02-26-000005-L1       6

2015-03             UA-2015-03-05-000001-L1       2
                    UA-2015-03-05-000003-L1       2
                    UA-2015-03-05-000004-L1       4
                    UA-2015-03-06-000009-L1       1
                    UA-2015-03-06-000010-L1       6
                              ...

I want to count number of id in each month. I have tried the variant below, but I get total number of rows in each month, not number of id's:

procs.groupby(['month'])['id'].count()

month
2015-02       35
2015-03      209
2015-04      442
2015-05      497
2015-06      933
           ...  

I need to get this:

month       number_id 
2015-02     6
           ...

Thanx for any help!

Upvotes: 0

Views: 50

Answers (2)

ipj
ipj

Reputation: 3598

Try:

procs.groupby(['month', 'id']).size().groupby(['month']).count()

Upvotes: 0

Omrum Cetin
Omrum Cetin

Reputation: 1469

As far as I understand from below comments you want group of groups :

procs.groupby(['month','id'])['month'].count().groupby(['month']).count()

Upvotes: 1

Related Questions