0
0
Pandasdata~10 mins

Ascending and descending order in Pandas - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Ascending and descending order
Start with DataFrame
Choose column(s) to sort
Apply sort_values()
Set ascending=True or False
Get sorted DataFrame
Use or display sorted data
We start with a DataFrame, pick columns to sort, apply sort_values() with ascending True or False, and get the sorted DataFrame.
Execution Sample
Pandas
import pandas as pd

df = pd.DataFrame({'Name': ['Anna', 'Bob', 'Cara'], 'Age': [25, 30, 22]})
sorted_df = df.sort_values(by='Age', ascending=True)
print(sorted_df)
This code sorts the DataFrame by the 'Age' column in ascending order and prints the result.
Execution Table
StepActionDataFrame StateResult
1Create DataFrame[{'Name': 'Anna', 'Age': 25}, {'Name': 'Bob', 'Age': 30}, {'Name': 'Cara', 'Age': 22}]Original DataFrame created
2Call sort_values(by='Age', ascending=True)Same as step 1DataFrame sorted by Age ascending: Cara(22), Anna(25), Bob(30)
3Print sorted DataFrameSorted DataFrameOutput shows rows ordered by Age ascending
4Call sort_values(by='Age', ascending=False)Same as step 1DataFrame sorted by Age descending: Bob(30), Anna(25), Cara(22)
5Print sorted DataFrameSorted DataFrameOutput shows rows ordered by Age descending
6EndNo changeSorting complete
💡 Sorting ends after printing sorted DataFrames in ascending and descending order.
Variable Tracker
VariableStartAfter Step 2After Step 4Final
df[Anna 25, Bob 30, Cara 22][Anna 25, Bob 30, Cara 22][Anna 25, Bob 30, Cara 22][Anna 25, Bob 30, Cara 22]
sorted_dfNone[Cara 22, Anna 25, Bob 30][Bob 30, Anna 25, Cara 22][Bob 30, Anna 25, Cara 22]
Key Moments - 2 Insights
Why does the original DataFrame 'df' not change after sorting?
Because sort_values() returns a new sorted DataFrame and does not modify 'df' unless you use inplace=True. See execution_table steps 2 and 4 where 'df' stays the same.
What does the 'ascending' parameter control?
It controls if the sorting is from smallest to largest (True) or largest to smallest (False). See execution_table steps 2 and 4 for ascending=True and ascending=False results.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2, what is the order of names in sorted_df?
ABob, Anna, Cara
BAnna, Bob, Cara
CCara, Anna, Bob
DCara, Bob, Anna
💡 Hint
Check the 'Result' column at step 2 in execution_table.
At which step does sorted_df show descending order by Age?
AStep 4
BStep 2
CStep 3
DStep 5
💡 Hint
Look for ascending=False in the 'Action' column in execution_table.
If we set inplace=True in sort_values(), what changes in variable_tracker?
A'df' would be deleted
B'df' would change after sorting steps
C'sorted_df' would be None
DNo change in any variable
💡 Hint
Recall that inplace=True modifies the original DataFrame, affecting 'df' in variable_tracker.
Concept Snapshot
pandas.DataFrame.sort_values(by=column, ascending=True)
- Sorts DataFrame rows by column values
- ascending=True for low to high, False for high to low
- Returns new sorted DataFrame unless inplace=True
- Use to organize data for analysis or display
Full Transcript
We start with a DataFrame containing names and ages. We use the pandas function sort_values() to order the rows by the 'Age' column. Setting ascending=True sorts from youngest to oldest. The function returns a new DataFrame sorted accordingly, leaving the original unchanged. Setting ascending=False sorts from oldest to youngest. This helps us see data in order we want without changing the original data unless we specify inplace=True. The variable tracker shows how the sorted DataFrame changes while the original stays the same. This step-by-step trace helps understand sorting in pandas clearly.