1

I have a data frame like this:

col1    col2   col3 
 A       B       4
 A       B       1
 A       C       2
 P       B       2
 P       D       7
 P       D       3

I want the minimum value of col3 for each combination of col1 and col2. The output will be like:

Col1    Col2    Col3
 A       B       1
 A       C       2
 P       B       2
 P       D       3

I have tried the code

df.groupby(['Col3']).min()

But It's not giving expected result. How to do this in efficient way ?

  • 2
    `df.groupby(['col1','col2'],as_index=False).min() ` or`df.sort_values('col3').drop_duplicated(['col1','col2'],keep='last')` – BENY Aug 16 '18 at 18:13
  • @Wen, Feel free to reopen this one to explain the 2 options. I know there's a better dup target but I can't find it :( – jpp Aug 16 '18 at 18:18
  • @jpp this must be a dup , I agree with you , just too lazy to find a dup . haha :-) – BENY Aug 16 '18 at 18:19
  • `df['col3'] = df.groupby(['col1','col2'])['col3'].transform('min')`, followed by `df = df.drop_duplicates()` – rahlf23 Aug 16 '18 at 18:54

0 Answers0