0
0
Pandasdata~10 mins

sort_values() by multiple columns in Pandas - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - sort_values() by multiple columns
Start with DataFrame
Choose columns to sort by
Specify ascending or descending for each
Call sort_values() with columns and order
DataFrame sorted by first column
Within ties, sorted by second column
Return sorted DataFrame
We start with a DataFrame, pick columns to sort by, specify order for each, then call sort_values(). The DataFrame is sorted first by the first column, then by the second within ties.
Execution Sample
Pandas
import pandas as pd

df = pd.DataFrame({
  'A': [2, 1, 2, 1],
  'B': [1, 2, 2, 1]
})

sorted_df = df.sort_values(by=['A', 'B'], ascending=[True, False])
Sort the DataFrame first by column 'A' ascending, then by column 'B' descending within ties.
Execution Table
StepDataFrame StateActionResult
1[{'A':2,'B':1},{'A':1,'B':2},{'A':2,'B':2},{'A':1,'B':1}]Start with original DataFrameDataFrame with 4 rows as given
2Same as step 1Choose columns to sort by: ['A', 'B']Ready to sort by 'A' then 'B'
3Same as step 1Specify ascending=[True, False]Sort 'A' ascending, 'B' descending
4Same as step 1Call df.sort_values(by=['A','B'], ascending=[True,False])Sort rows by 'A' ascending and 'B' descending within ties
5[{'A':1,'B':2},{'A':1,'B':1},{'A':2,'B':2},{'A':2,'B':1}]Within ties of 'A', sort by 'B' descendingRows with A=1 sorted by B descending: 2 then 1 Rows with A=2 sorted by B descending: 2 then 1
6[{'A':1,'B':2},{'A':1,'B':1},{'A':2,'B':2},{'A':2,'B':1}]Return sorted DataFrameFinal sorted DataFrame
💡 Sorting complete: DataFrame sorted by 'A' ascending and 'B' descending within ties
Variable Tracker
VariableStartAfter Step 4After Step 5Final
df[{'A':2,'B':1},{'A':1,'B':2},{'A':2,'B':2},{'A':1,'B':1}][{'A':2,'B':1},{'A':1,'B':2},{'A':2,'B':2},{'A':1,'B':1}][{'A':2,'B':1},{'A':1,'B':2},{'A':2,'B':2},{'A':1,'B':1}][{'A':2,'B':1},{'A':1,'B':2},{'A':2,'B':2},{'A':1,'B':1}]
sorted_dfNone[{'A':1,'B':2},{'A':1,'B':1},{'A':2,'B':2},{'A':2,'B':1}][{'A':1,'B':2},{'A':1,'B':1},{'A':2,'B':2},{'A':2,'B':1}][{'A':1,'B':2},{'A':1,'B':1},{'A':2,'B':2},{'A':2,'B':1}]
Key Moments - 3 Insights
Why does the sorting first order by column 'A' before column 'B'?
Because in the execution_table step 4, sort_values() uses the 'by' list order. It sorts first by 'A', then breaks ties by sorting 'B' as shown in step 5.
How does specifying ascending=[True, False] affect the sorting?
As seen in step 3 and 5, 'A' is sorted ascending (small to large), while 'B' is sorted descending (large to small) within groups of equal 'A'.
Does the original DataFrame 'df' change after sorting?
No, as shown in variable_tracker, 'df' remains unchanged. The sorted DataFrame is returned as 'sorted_df'.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table step 5, what is the order of rows with 'A' equal to 2?
A[{'A':2,'B':2}, {'A':2,'B':1}]
B[{'A':2,'B':1}, {'A':2,'B':1}]
C[{'A':2,'B':1}, {'A':2,'B':2}]
D[{'A':2,'B':2}, {'A':2,'B':2}]
💡 Hint
Check the 'Within ties' sorting in step 5 of execution_table where 'B' is descending for 'A' = 2
At which step does the DataFrame get sorted by the first column 'A'?
AStep 2
BStep 4
CStep 5
DStep 6
💡 Hint
Look at execution_table step 4 where sort_values() is called and sorting by 'A' happens
If ascending=[False, True] was used instead, how would the order of rows with 'A' = 1 change?
ARows with 'A' = 1 would not change order
BRows with 'A' = 1 would be sorted by 'B' descending (2 then 1)
CRows with 'A' = 1 would be sorted by 'B' ascending (1 then 2)
DRows with 'A' = 1 would be sorted by 'A' descending
💡 Hint
Refer to how ascending list controls order per column in execution_table steps 3 and 5
Concept Snapshot
sort_values(by=[col1, col2], ascending=[bool1, bool2])
- Sorts DataFrame by multiple columns in order
- First sorts by col1, then col2 within ties
- ascending list controls order per column
- Returns new sorted DataFrame, original unchanged
Full Transcript
This visual execution shows how pandas sort_values() works when sorting by multiple columns. We start with a DataFrame with columns 'A' and 'B'. We choose to sort by 'A' first, then 'B'. We specify ascending order for 'A' as True and for 'B' as False. The sorting happens in steps: first by 'A' ascending, then within equal 'A' values by 'B' descending. The original DataFrame remains unchanged, and a new sorted DataFrame is returned. Key moments include understanding the order of sorting columns, how ascending list affects each column, and that the original data is not modified. The quizzes test understanding of sorting order and effects of changing ascending flags.