Basically the same as Select first row in each GROUP BY group? only in pandas.
df = pd.DataFrame({'A' : ['foo', 'foo', 'foo', 'foo', 'bar', 'bar', 'bar', 'bar'], 'B' : ['3', '1', '2', '4','2', '4', '1', '3'], })
Sorting looks promising:
df.sort('B') A B 1 foo 1 6 bar 1 2 foo 2 4 bar 2 0 foo 3 7 bar 3 3 foo 4 5 bar 4
But then first won't give the desired result... df.groupby('A').first()
B A bar 2 foo 3
Select & print first row of dataframe using head() It will return the first row of dataframe as a dataframe object. Using the head() function, we fetched the first row of dataframe as a dataframe and then just printed it.
Pandas DataFrame first() Method The first() method returns the first n rows, based on the specified value. The index have to be dates for this method to work as expected.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With