0

I have a dataframe that looks like this -

   P_id Sim no_of_occurance
0   1   0.00    4
1   2   0.25    3
2   3   0.25    5
3   4   0.25    5
4   5   0.25    6
5   6   0.25    4
6   7   0.50    6
7   8   0.50    2
8   9   0.50    5
9   10  0.25    4
10  11  0.25    6
11  12  0.50    4
12  13  0.50    4
13  14  0.25    4

I want to sort P_id based on the corresponding descending values in Sim' column for each row and for rows where Sim values are same, I want that P_id to come first whose no_of_occurance is higher.

Desired output-

P_id  Sim    no_occurance
 7    0.50    6
 9    0.50    5
 12   0.50    4
 13   0.50    4
 8    0.50    2
 5    0.25    6
 11   0.25    6
 .
 .
 .
1
  • 1
    You are looking for pandas.DataFrame.sort_values. Here is a link Commented Jul 12, 2021 at 16:05

1 Answer 1

2

Try:

df = df.sort_values(by=["Sim", "no_of_occurance"], ascending=[False, False])
print(df)

Prints:

    P_id   Sim  no_of_occurance
6      7  0.50                6
8      9  0.50                5
11    12  0.50                4
12    13  0.50                4
7      8  0.50                2
4      5  0.25                6
10    11  0.25                6
2      3  0.25                5
3      4  0.25                5
5      6  0.25                4
9     10  0.25                4
13    14  0.25                4
1      2  0.25                3
0      1  0.00                4
Sign up to request clarification or add additional context in comments.

2 Comments

i think df.sort_values(by =["Sim", "no_of_occurance"],ascending=False ) , do we need False two times?
@KarnKumar Yes, one False is enought. I wanted to be just explicit.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.