0
import pandas as pd

records = [
    {"a": 10, "b": 22},
    {"a": 11, "b": 21},
    {"a": 60, "b": 200},
    {"a": 70, "b": 40},
    {"a": 10, "b": 30},
]

def sort(c):
   # TODO

df = pd.DataFrame.from_records(records)

df.sort_values(["a", "b"], ascending=[True, False], inplace=True, key=sort)

print(df.head())

Prints

    a    b
4  10   30
0  10   22
1  11   21
2  60  200
3  70   40

I want to sort the dataframe by the two columns at the same time - not necessarily respecting data integrity; for example, the result of column b should be on the top because of its value, which is 200. sort_values will prioritize the columns in the given order but I want to sort each column individually.

I know I can have a custom function for sorting, but which algorithm should I use in this case?

Nuri Taş
  • 3,828
  • 2
  • 4
  • 22
Rodrigo
  • 135
  • 4
  • 45
  • 107

0 Answers0