LearnToGrow
LearnToGrow

Reputation: 1779

How to select columns on pandas groupby

data = {'user_id':[1, 2, 3, 1, 2, 3, 4, 5, 2, 10, 8, 9, 6, 6,7], 'diploma':['BS', 'BS', 'BS', 'Msc', 'Msc', 'Msc',
                                                                           'BS', 'BS', 'BS', 'Phd', 'BS', 'BS',
                                                                            'BS', 'BS', 'BS']}
df = pd.DataFrame(data)

groupby_result = df.groupby('user_id').count()
groupby_result

only_two_diploma = groupby_result[groupby_result['diploma']>=2]

        diploma
user_id     
    1   2
    2   3
    3   2
    6   2

How to select the user_id only_two_diploma dataframe?

Upvotes: 0

Views: 58

Answers (1)

GabrielP
GabrielP

Reputation: 782

You could use the following:

selected_users = list(groupby_result[groupby_result['diploma']>2].index)

Upvotes: 1

Related Questions