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

Sort multiple `by` columns with equation in key

Would like to sort a df with an equation in the key as follows:

import pandas as pd
import numpy as np
df = pd.DataFrame({'name': ['x', 'y', 'z'], 
                   'price': [1, 2, np.nan], 
                   'cost': [3, np.nan, 1]})

df.sort_values(by=['price', 'cost'], key=lambda x: x.price/x.cost)
…doesn’t work and gives the error AttributeError: 'Series' object has no attribute 'price'

What’s the right way?

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

>Solution :

Better compute the ratio, sort this and reindex:

out = df.loc[df['price'].div(df['cost']).sort_values().index]

Or, if you have tricky indices, with numpy.argsort:

out = df.iloc[np.argsort(df['price'].div(df['cost']))]

Output:

  name  price  cost
0    x    1.0   3.0
1    y    2.0   NaN
2    z    NaN   1.0
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