Andrew Andrew - 3 years ago 290
Python Question

pandas: how to select unique rows in group

I have a Pandas grouped dataframe with a column called 'PART_ID' in all groups and there may be duplicates of the value in this column within each group. I want to unique-ify within each group based in the value of this column and retain only the unique ones.

I thought it would simply be a case of .unique() on the grouped object but this doesn't work. There is no set option either. How do I get the unique rows within the groups?. E.g grouping on column 1

A 10

A 10

A 12

B 10

B 15

should give

A 10

A 12

B 10

B 15

Answer Source

Using .unique()

grouped_df['column_1'].unique()

or without unique you could do something like...

grouped_df['column_1'].apply(list).apply(set)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download