sort_values()

noun

say it: “SORT VAL-yooz” /ˈsɔrt ˈvæljuz/

A pandas method that puts a table's rows in order by the values in one or more columns, smallest first unless told otherwise.

What does sort_values() do in pandas?

sort_values() is the pandas method that hands back a table with its rows in order by one column, or by several, with ascending= saying which way each one goes. It puts the smallest first unless you say otherwise.

import pandas as pd
products = pd.DataFrame({"name": ["Trail Boot", "Rain Jacket", "Camp Stove"],
                         "rating": [4.6, 4.8, 4.6], "reviews": [210, 95, 340]})
best = products.sort_values(["rating", "reviews"], ascending=[False, False])
print(best["name"].tolist())   # ['Rain Jacket', 'Camp Stove', 'Trail Boot']

In a sentence

  • “sort_values() put the best-rated products at the top.”
  • “She used sort_values() with ascending=False to list the biggest orders first.”

Level: Apprentice · More Python terms at this level

Updated