In pandas, how do you sort a DataFrame by one column in descending order, and where do missing values end up?
answer
- a new frame comes back
- one flag flips the direction
- missing values have their own argument
- labels travel with their rows
- ignore_index renumbers from zero
basics
~10 sCall df.sort_values('revenue', ascending=False), which returns a new DataFrame; missing values go last in either direction unless na_position='first', and every row keeps its original index label.
solid answer
~40 s`df.sort_values('revenue', ascending=False)` returns a new, sorted DataFrame; `df` itself keeps its old order unless you assign the result back, which is the slip interviewers watch for. Missing values are placed by `na_position`, whose default `'last'` holds in **both** directions, so NaN revenues sink to the bottom of a descending leaderboard too; pass `na_position='first'` to surface them. Each row keeps its index label, so after the sort `board.loc[0]` still finds the record that used to be first while `board.iloc[0]` finds the new top seller. Pass `ignore_index=True` (or chain `reset_index(drop=True)`) when you want labels 0 to n-1, and chain `.head(10)` for a top ten.
code
python · 14 linesimport pandas as pd
import numpy as np
sales = pd.DataFrame({
"product": ["Kettle", "Toaster", "Blender", "Mixer", "Juicer"],
"revenue": [1200.0, 950.0, np.nan, 1400.0, 300.0],
})
board = sales.sort_values("revenue", ascending=False)
print(board)
print(board.loc[0, "product"], "|", board.iloc[0]["product"])
print(sales.sort_values("revenue", ascending=False,
na_position="first", ignore_index=True))go deeper
Recall the exact call, sort_values with ascending=False, and say out loud that it returns a new frame you must assign.
Explain na_position and why its default does not follow the direction, and how labels travel with rows so loc and iloc disagree after a sort.
Show the production habits: assign rather than inplace, decide deliberately where missing revenue belongs in a report, and reset labels before exporting a ranked table.
Frame sorting as a presentation step: agree on where missing values and tied rows land so every report built from the same frame orders them the same way.
## The call and what it returns `DataFrame.sort_values` reorders the **rows** of a pandas DataFrame by the values in one or more columns. For a sales leaderboard you name the column in `by` (the first positional argument) and reverse the direction with `ascending=False`: ```python board = sales.sort_values("revenue", ascending=False) ``` The facts a hands-on round checks: - The method **returns a new DataFrame**. `sales` is not reordered. A line that calls `sales.sort_values(...)` without assigning the result, followed by `print(sales)`, shows the old order — the single most common slip with this method. - `inplace=True` reorders `sales` itself and returns `None`, so `board = sales.sort_values("revenue", inplace=True)` leaves `board` holding `None`. Assigning the returned frame is the idiomatic style and chains cleanly. - `ascending` defaults to `True` (smallest first). One `False` reverses the order. - `Series.sort_values` behaves the same way on a single column and simply has no `by`. ## Where missing values go A **missing value** (NaN in a float column, NaT in a datetime column) cannot be compared with real numbers, so pandas sets those rows aside and attaches them to one end of the result. The `na_position` argument decides which end: | `na_position` | with `ascending=True` | with `ascending=False` | |---|---|---| | `'last'` (default) | bottom | bottom | | `'first'` | top | top | The point worth stating aloud: the default does **not** follow the direction. A descending sort does not lift NaN to the top, as it would if NaN behaved like a very large number. For a leaderboard that is usually right — a product with no recorded revenue should not look like the best seller. When the missing rows are exactly what you need to inspect, `na_position="first"` puts them in front. ## What happens to the index Sorting moves whole rows, and each row keeps its **index label** (the row label pandas prints on the left). After a sort the labels read in a scrambled order such as `3, 0, 1, 4, 2`. Two consequences follow: 1. **Label access and position access now disagree.** `board.loc[0]` still returns the record that was labelled 0 before the sort, wherever it now sits, while `board.iloc[0]` returns whatever row is physically first — the top seller. 2. **Exports show the old labels.** Printing or writing the frame with its index shows labels that look like ranks but are not. When you want fresh labels, pass `ignore_index=True`, which labels the result `0, 1, …, n - 1`, or chain `.reset_index(drop=True)`. Keeping the labels has a use too: if the original index was already in order (the default `RangeIndex` is), `board.sort_index()` puts the rows back exactly as they were. ## Taking the top of the sorted frame `.head(n)` after a descending sort gives a top-n list: `sales.sort_values("revenue", ascending=False).head(3)`. When the only goal is the top n rows, `DataFrame.nlargest` selects them without ordering every row, and it has its own rules for ties at the cut-off. ## Common slips - Calling `sort_values` without assigning the result, then concluding the sort "did nothing". - Expecting NaN to follow the direction and appear at the top of a descending sort. - Fetching the "first" row with `.loc[0]` after sorting. - Sorting numbers stored as text: `"1200"` comes before `"950"` because text compares character by character. Convert with `pd.to_numeric` or `astype` before sorting. - Assuming tied rows keep their original order. With the default `kind="quicksort"` pandas makes no such promise; `kind="stable"` is the argument that does. ## A checklist before sharing a sorted table 1. **Assigned?** The sorted frame is held in a variable, not lost on a line of its own. 2. **Missing values placed on purpose?** Decide whether rows with no revenue belong at the bottom (the default) or at the top for review, and say so in the report. 3. **Labels intended?** Either keep the original labels deliberately, so records can be traced back, or pass `ignore_index=True` so the printed labels run `0` to `n - 1` and nobody mistakes them for ranks. 4. **Right dtype?** `df.dtypes` shows numbers as `int64` or `float64`, not text, before you trust a numeric order. A good interview answer names the call, says that a new frame comes back, and volunteers the NaN placement and the index labels before being asked — those two details are where a quick leaderboard goes wrong in practice.
- After the sort, why does board.loc[0] not return the top seller?`.loc` looks rows up by **label**, and sorting moves each row together with its label. Label 0 belongs to whichever record was labelled 0 before the sort, wherever it now sits. `.iloc[0]` looks up by **position**, so it returns the row that is physically first: the top seller. Pass `ignore_index=True` if labels should match positions.
- How do you get back to the original row order after several sorts?If the original index was in order, as the default `RangeIndex` is, call `sort_index()`: the labels travelled with the rows, so sorting by label restores the original sequence. If you used `ignore_index=True` along the way, that information is gone, so keep an explicit order column before sorting.
- Why might a revenue of '1200' sort before '950'?The column holds text, not numbers. Text compares character by character, and `'1'` comes before `'9'`, so `'1200'` sorts first. Check `df.dtypes`, convert with `pd.to_numeric(df['revenue'])` or `astype(float)`, and sort again.
- sort_values reorders the DataFrame it is called on by default
- A descending sort puts NaN values at the top
- After sorting, df.loc[0] returns the first row of the result
- Sorting renumbers the index from zero automatically
- sort_values with inplace=True returns the sorted DataFrame for chaining