PhishFood
PhishFood

Reputation: 71

Count and groupby in Python

I have created the following code which counts the amount of times a person (by their login_id) has logged into a program over a period of a year.

data1.query("'2015-12-01' <= login_date <= '2016-12-01'").groupby(['employer_key','account_id']).size().reset_index().groupby(['employer_key','account_id'])[[0]].count()

The output looks something like this:

employer_key   account_id  # times logged in
 Apple            X1             1
 Google           Y5             2
 Facebook         X3             4
 Apple            X2             2
 Facebook         Y2             1

I would like to count the number of account_ids for each separate employer_key, so that I can determine how many accounts logged in for each individual employer over a period of a year.

The output would hopefully look something like this:

employer_key   user_logins
 Apple            2             
 Google           1             
 Facebook         2            

Upvotes: 1

Views: 155

Answers (1)

DYZ
DYZ

Reputation: 57145

I guess this should work:

data.groupby(['employer_key','account_id']).count().\
             unstack().sum(axis=1).astype(int)
#employer_key
#Apple       2
#Facebook    2
#Google      1
#dtype: int64

Upvotes: 1

Related Questions