-
-
Notifications
You must be signed in to change notification settings - Fork 19.4k
Closed
Labels
GroupbyNeeds InfoClarification about behavior needed to assess issueClarification about behavior needed to assess issue
Description
Code Sample, a copy-pastable example if possible
Current method
import pandas as pd
dataframe = pd.DataFrame({'col1': list('aaabbbcc'), 'col2': range(8), 'col3': list('whatever')})
dataframe.groupby('col1').apply(lambda df: df.nlargest(2, 'col2'))Desired method
import pandas as pd
dataframe = pd.DataFrame({'col1': list('aaabbbcc'), 'col2': range(8), 'col3': list('whatever')})
dataframe.groupby('col1').nlargest(2, 'col2')Problem description
I want to select the first 2 entries of each group ordered by a specific column. I can easily get these values if I only want to keep the column used for the ordering (see code below) but it gets way less efficient and not as clear when I want to keep all the columns of the DataFrame.
import pandas as pd
dataframe = pd.DataFrame({'col1': list('aaabbbcc'), 'col2': range(8), 'col3': list('whatever')})
dataframe.groupby('col1').col2.nlargest(2)karldw and karthikbhamidipati
Metadata
Metadata
Assignees
Labels
GroupbyNeeds InfoClarification about behavior needed to assess issueClarification about behavior needed to assess issue