I have a pandas dataframe that looks like this:
Name Candidates Qualifier Score AAA AAA_1 Yes 0 AAA AAA_2 Yes 10 AAA AAA_3 No 20 BBB BBB_1 No 1 BBB BBB-2 Yes 10 BBB BBB_3 Yes 50
I want to choose the best two candidates in each "name" with the highest score. How can i do this?
You can sort df by "Name" and "Score", and then groupbyby "Name" and call head(2)to get the first 2 lines for each group:
groupby
head(2)
In [228]: df.sort(['Name','Score'], ascending=False).groupby('Name').head(2) Out[228]: Name Candidates Qualifier Score 5 BBB BBB_3 Yes 50 4 BBB BBB-2 Yes 10 2 AAA AAA_3 No 20 1 AAA AAA_2 Yes 10
, , EdChum answer, , . , sort.
sort
Sorted = df.sort(['Score'], ascending = False).groupby('Name').head(2) print Sorted.sort(['Candidate'], ascending = True) Name Cand Score 1 AAA AAA_2 10 2 AAA AAA_3 20 4 BBB BBB_2 10 5 BBB BBB_3 50
Source: https://habr.com/ru/post/1611456/More articles:Attempting to call a function every 24 hours - javaSet the default value for a Postgres JSON column in Rails <4 - jsonВход в Google не делает ничего, когда сторонние файлы cookie отключены - javascriptДолжны ли шаблоны находиться в папках шаблонов шаблонов или глобальной папке шаблона? - pythonPython print format using NumPy - pythonThe correct way to detect that a UWP application is running on a small screen device (phone) - c #save notification on lock screen after unlocking - iosВариации Sku не отображаются Wordpress/woocommerce - phpKeep notification on lock screen - iosSchematic: difference between define and define-syntax-rule - macrosAll Articles