Sort by One or Many Columns
Ascending, descending, and ties.
Sort by One or Many Columns is a free Data Science Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Data Science Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Order Brings Insight
Raw rows arrive in no useful order. Sorting puts the most interesting records on top so patterns jump out at a glance. 🔎
sort_values Is Your Tool
In pandas you reorder rows with sort_values. You name the column to sort by, and pandas hands back a freshly ordered DataFrame.
df.sort_values("price")Ascending by Default
By default sort_values goes smallest to largest. So numbers climb upward and text sorts alphabetically from A to Z.
Flip to Descending
Set ascending=False when you want the biggest values first, like the top-selling products or the highest scores.
df.sort_values("sales", ascending=False)Sort by Many Columns
Pass a list of columns and pandas sorts by the first, then breaks ties using the next, and so on down the list.
df.sort_values(["region", "sales"])Mixed Sort Directions
Give ascending a list too, one flag per column. That lets you sort region A to Z while sales go high to low.
df.sort_values(["region", "sales"], ascending=[True, False])A New Frame, Not In Place
By default sort_values returns a sorted copy and leaves the original untouched, so your source data stays safe.
Sort In Place If You Mean It
Add inplace=True to rewrite the original DataFrame instead of returning a copy. Use it only when you truly want that.
Watch the Index
Sorting keeps each row label glued to its data, so the index looks shuffled afterward. That is expected, not a bug.
Reset for Clean Numbers
Want tidy 0,1,2 labels after sorting? Chain reset_index with drop=True to renumber rows from the top.
df.sort_values("sales").reset_index(drop=True)Where NaN Values Land
Missing values sink to the bottom by default. Set na_position to first if you would rather see them up top.
Quick Check
You want the highest sales on top. How do you sort?
Recap: Sorting Made Simple
You now sort with sort_values, flip with ascending, break ties using a column list, and tidy labels with reset_index. Nicely done! 🎉
Frequently asked questions
Is the “Sort by One or Many Columns” lesson free?
Yes — the full text of “Sort by One or Many Columns” is free to read here on the web, and the Data Science Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Data Science Academy course, upgrade to CoddyKit PRO.
What will I learn in “Sort by One or Many Columns”?
Ascending, descending, and ties. You practise Data Science Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Data Science Academy?
No prior experience is required. Data Science Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Sort by One or Many Columns” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Data Science Academy lesson?
Yes. Every Data Science Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Sort by One or Many Columns
- Your First groupby Summary
- Line and Bar Charts in matplotlib
- Labels, Titles, and Legends