Reddspark
Reddspark

Reputation: 7567

Pandas equiv of "Select x from y groupby x"?

I have this code

dfUsers = df.groupby('UserID').count()

which gives me back a list of user IDs plus counts. It works for what I need it for but was wondering if there was better code out there for getting back just the unique list of userIDs, and not the count, from a dataframe that has users appearing more than once?

The SQL equivalent would be: Select userID from dataframe groupBy userID

Upvotes: 1

Views: 66

Answers (1)

guigen
guigen

Reputation: 91

Use unique:

dfUsers = df['UserID'].unique()

Alternatively, you can use drop_duplicates which returns a DataFrame:

dfUsers = df['UserID'].drop_duplicates()

Upvotes: 4

Related Questions