I want to select rows by the maximum values of another column which would be the duplicated rows containing duplicated maximum values of a group.

This should contain three steps:
(1) group dataframe by column A;
(2) get duplicated rows with duplicated maximum values of column B;
(3) get rows if it contains maximum values of column C (if it is still duplicated, pick the first).


df_test = pd.DataFrame({'A':[1,1,2,3,4,2,4,3,3,2],

enter image description here


enter image description here


1 Answer 1


This is more of a programming than a data science question, and would therefore be better suited for stackoverflow, but this can be achieved relatively easily using a combination of sorting and grouping:

    # sort such that the first row is within each group is the one you want
    .sort_values(["A", "B", "C"], ascending=[True, False, False])
    # group based on column A
    # select the first row within each group
    # reset the index such that A is a column instead of the index

Which gives the following result:

1 3 85
2 3 70
3 4 90
4 5 85

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.