pandas: how do I select first row in each GROUP BY group?

Generally if you want your data sorted in a groupby but it’s not one of the columns which are going to be grouped on then it’s better to sort the df prior to performing groupby:

In [5]:
df.sort_values('B').groupby('A').first()

Out[5]:
     B
A     
bar  1
foo  1

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)