1

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            
4
  • How many unique accounts? Commented Mar 6, 2017 at 23:52
  • In my actual data set I have thousands. Commented Mar 6, 2017 at 23:55
  • No, my question is if you want to count unique users per employer. I think your data may already have only unique account_id in that column, so it may be a moot point. Commented Mar 6, 2017 at 23:57
  • Oh sorry. Yes, all the account_ids are unique. Commented Mar 7, 2017 at 0:02

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

3 Comments

How is my above code factored into the code you provided? When I run your code, it returns me data for all dates, not for my specified one year period. I am not sure how to integrate the one year period into your code. Thanks!
My answer is applicable to the output of your code, as shown in your first output block.
What specific part linking the two codes? Thanks so much for your help, I really appreciate it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.