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?
>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