Get rows of max values for unique value in other column: python

windwalker

I am trying to filter the following dataframe from the view seen in 'Initial dataframe' to what is displayed in 'Desired output'

Initial dataframe

name          group     subject  score   class_size
                                 
Steve       classrm_A   maths    98.22      20
John        classrm_A   maths    76.87      30
Mary        classrm_C   science  77.25      26
Steve       classrm_B   science  65.28      32
Mary        classrm_A   english  86.01      16
John        classrm_F   science  96.55      25

Return rows for unique 'name' values where score' is greatest and 'class_size' is equal or greater than 25.

Desired output:

name          group     subject  score   class_size
                                 
Steve       classrm_B   science  65.28      32
Mary        classrm_C   science  77.25      26
John        classrm_F   science  96.55      25

Here is what I have attempted so far.....

min_class = df["class_size"] >= 25


df = df["min_class "]


df = df.groupby(['name']).max('score')

Any help would be greatly appreciated.

Andreas

Keep only rows with class size above 25 in dataframe, then sort dataframe by score, then drop all duplicates of column "name" and keep only the first row in case of duplicates.

df = df[df["class_size"] >= 25]
df = df.sort_values("score", ascending=False)
df = df.drop_duplicates(subset=["name"], keep="first")

Output:

Out[23]: 
    name      group  subject  score  class_size
5   John  classrm_F  science  96.55          25
2   Mary  classrm_C  science  77.25          26
3  Steve  classrm_B  science  65.28          32

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

sql max partition by to get other column value

From Dev

How to Get Max Value and Return Other Column

From Dev

How to transform rows of other columns to columns on the basis of unique values of a column?

From Dev

Get values in one column that correspond with max value of other columns in a matrix (R)?

From Dev

Get values in one column that correspond with max value of other columns in a matrix (R)?

From Dev

Query to fetch Unique value of a column and dulplicate values of other column

From Dev

Python/CSV unique rows with unique values per row in a column

From Dev

How to get rows with the max value by using Python?

From Dev

How to convert column values to rows for each unique value in a dataframe in R?

From Dev

Python : How to rename the column name that changes with the unique value in other column?

From Dev

Hibernate criteria how to get rows with max column value

From Dev

Get rows where one column is unique and another column is the lowest value relative to the unique column

From Dev

Combining rows on a Dataframe based on a specific column value and add other values

From Dev

Drop pandas rows if value is not between two other values on the same column

From Dev

Python Pandas - Only showing rows in DF for the MAX values of a column

From Dev

Finding the rows which has a unique column values in python

From Dev

get max value of column

From Dev

Python PANDAS: New Column, Apply Unique Value To All Rows

From Dev

How to get rows with min values in one column, grouped by other column, while keeping other columns?

From Dev

How to get rows with min values in one column, grouped by other column, while keeping other columns?

From Dev

Pandas, for each unique value in one column, get unique values in another column

From Dev

How can I apply a unique value to column A for rows with common values in column A

From Dev

SQL: How to get a column's values of all rows as a single value?

From Dev

How to get average value (of column values) of a category (distributed in rows)?

From Dev

SQL - Get rows where all values X of Column A = value B

From Dev

How to get distinct values in other columns per value in the primary column

From Dev

How to get distinct values in other columns per value in the primary column

From Dev

group by on one column and get the max on other column

From Dev

How to select rows from MySQL based on max value of a one column and grouping two other columns?

Related Related

  1. 1

    sql max partition by to get other column value

  2. 2

    How to Get Max Value and Return Other Column

  3. 3

    How to transform rows of other columns to columns on the basis of unique values of a column?

  4. 4

    Get values in one column that correspond with max value of other columns in a matrix (R)?

  5. 5

    Get values in one column that correspond with max value of other columns in a matrix (R)?

  6. 6

    Query to fetch Unique value of a column and dulplicate values of other column

  7. 7

    Python/CSV unique rows with unique values per row in a column

  8. 8

    How to get rows with the max value by using Python?

  9. 9

    How to convert column values to rows for each unique value in a dataframe in R?

  10. 10

    Python : How to rename the column name that changes with the unique value in other column?

  11. 11

    Hibernate criteria how to get rows with max column value

  12. 12

    Get rows where one column is unique and another column is the lowest value relative to the unique column

  13. 13

    Combining rows on a Dataframe based on a specific column value and add other values

  14. 14

    Drop pandas rows if value is not between two other values on the same column

  15. 15

    Python Pandas - Only showing rows in DF for the MAX values of a column

  16. 16

    Finding the rows which has a unique column values in python

  17. 17

    get max value of column

  18. 18

    Python PANDAS: New Column, Apply Unique Value To All Rows

  19. 19

    How to get rows with min values in one column, grouped by other column, while keeping other columns?

  20. 20

    How to get rows with min values in one column, grouped by other column, while keeping other columns?

  21. 21

    Pandas, for each unique value in one column, get unique values in another column

  22. 22

    How can I apply a unique value to column A for rows with common values in column A

  23. 23

    SQL: How to get a column's values of all rows as a single value?

  24. 24

    How to get average value (of column values) of a category (distributed in rows)?

  25. 25

    SQL - Get rows where all values X of Column A = value B

  26. 26

    How to get distinct values in other columns per value in the primary column

  27. 27

    How to get distinct values in other columns per value in the primary column

  28. 28

    group by on one column and get the max on other column

  29. 29

    How to select rows from MySQL based on max value of a one column and grouping two other columns?

HotTag

Archive