I need to sort a dataframe by a custom aggregation function, for example, the sum of their values, similarly to the sorted function with the key argument:
sorted([(1, 10), (1, 2), (2, 3)], key=sum)
which gives:
[(1, 2), (2, 3), (1, 10)]
I know that in pandas I could create a new aggregate column and sort by column:
df = pd.DataFrame([(1, 10), (1, 2), (2, 3)])
df[2] = df.sum(axis=1)
df.sort_values(2).drop(2, axis=1)
But as you can see it is way less elegant than the python solution with sorted().
Since sort_values() does not take a key argument, what would be a way to sort values in a dataframe by key without creating new columns?