Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Pandas: Sort by sum of 2 columns

I have a DataFrame:

COL1 COL2
   1    1
   3    1
   1    3

I need to sort by COL1 + COL2.

key=lambda col: f(col) argument-function of sort_values(…) lets you sort by a changed column but in the described case I need to sort on the basis of 2 columns. So, it would be nice if there were an opportunity to provide a key argument-function for 2 or more columns but I don’t know whether such a one exists.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

So, how can I sort its rows by sum COL1 + COL2?

Thank you for your time!

>Solution :

This does the trick:

data = {"Column 1": [1, 3, 1], "Column 2": [1, 2, 3]}
df = pd.DataFrame(data)

sorted_indices = (df["Column 1"] + df["Column 2"]).sort_values().index

df.loc[sorted_indices, :]

I just created a series that has the sum of both the columns, sorted it, got the sorted indices, and printed those indices out for the dataframe.

(I changed the data a little so you can see the sorting in action. Using the data you provided, you wouldn’t have been able to see the sorted data as it would have been the same as the original one.)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading