1

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

Andrew
  • 13
  • 1
  • 3
  • 3
    Seen https://stackoverflow.com/questions/12322779/pandas-unique-dataframe? – Zero Sep 30 '17 at 16:30
  • 3
    and https://stackoverflow.com/questions/30530663/how-to-select-distinct-across-multiple-data-frame-columns-in-pandas? – Zero Sep 30 '17 at 16:31
  • 1
    `drop_duplicates` :) – BENY Sep 30 '17 at 16:35
  • the issue here is the groupby. I have done a couple of operations that need to be done on groups and now need to unique-ify. drop_duplicates and unique don't work on groupby objects – Andrew Sep 30 '17 at 16:52
  • how to group by the first column and take average of the second column? like the output should be A (10+10+12)/3 and so on – devssh Jul 02 '18 at 14:00

1 Answers1

2

Using .unique()

grouped_df['column_1'].unique()

or without unique you could do something like...

grouped_df['column_1'].apply(list).apply(set)
Yale Newman
  • 1,141
  • 1
  • 13
  • 22