Ascending and descending order in Pandas - Time & Space Complexity
When we sort data in pandas, it takes some time depending on how much data there is.
We want to know how the time needed changes as the data grows.
Analyze the time complexity of the following code snippet.
import pandas as pd
df = pd.DataFrame({
'numbers': [5, 2, 9, 1, 7]
})
sorted_df = df.sort_values(by='numbers', ascending=True)
This code sorts a column of numbers in ascending order using pandas.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The sorting algorithm compares and rearranges elements.
- How many times: It repeats comparisons many times depending on the number of rows.
As the number of rows grows, the sorting work grows faster than just adding more rows.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 30 to 40 comparisons |
| 100 | About 700 to 1000 comparisons |
| 1000 | About 10,000 to 15,000 comparisons |
Pattern observation: The work grows faster than the number of rows, roughly like n log n.
Time Complexity: O(n log n)
This means if you double the data size, the time needed grows a bit more than double but much less than square.
[X] Wrong: "Sorting takes the same time no matter how many rows there are."
[OK] Correct: Sorting compares many pairs of rows, so more rows mean more comparisons and more time.
Understanding how sorting time grows helps you explain your code choices clearly and shows you know how data size affects performance.
"What if we sorted by multiple columns instead of one? How would the time complexity change?"
Practice
ascending=False do when sorting a pandas DataFrame?Solution
Step 1: Understand the ascending parameter
Theascendingparameter controls the order of sorting:Truemeans smallest to largest,Falsemeans largest to smallest.Step 2: Apply the meaning of
Settingascending=Falseascending=Falsesorts the data from the largest values down to the smallest values.Final Answer:
Sorts the data from largest to smallest values -> Option AQuick Check:
ascending=False = largest to smallest [OK]
- Confusing ascending=False with ascending=True
- Thinking it sorts alphabetically only
- Assuming it leaves data unsorted
df by column 'age' in ascending order?Solution
Step 1: Recall the correct pandas sorting method
The correct method to sort a DataFrame by column issort_values()with the parameterby='column_name'.Step 2: Check the ascending parameter and syntax
To sort ascending, useascending=True. df.sort_values(by='age', ascending=True) uses the correct method and parameters.Final Answer:
df.sort_values(by='age', ascending=True) -> Option CQuick Check:
sort_values(by=col, ascending=True) is correct [OK]
- Using df.sort instead of df.sort_values
- Mixing ascending True/False values
- Omitting 'by=' keyword
import pandas as pd
df = pd.DataFrame({'name': ['Anna', 'Bob', 'Cara'], 'score': [88, 92, 85]})
print(df.sort_values(by='score', ascending=False))What will be the output?
Solution
Step 1: Understand the sorting code
The code sorts the DataFrame by the 'score' column withascending=False, meaning from highest to lowest score.Step 2: Predict the sorted order
Scores are 88, 92, 85. Sorted descending: 92 (Bob), 88 (Anna), 85 (Cara).Final Answer:
Rows sorted by score from highest to lowest -> Option AQuick Check:
ascending=False sorts descending [OK]
- Thinking ascending=False sorts ascending
- Confusing sorting by name vs score
- Expecting an error from ascending=False
df.sort_values(by='height', ascending='False')
Solution
Step 1: Check the type of ascending parameter
Theascendingparameter expects a boolean value:TrueorFalse, not a string.Step 2: Identify the error in the code
Here,ascending='False'is a string, which will cause unexpected behavior or error.Final Answer:
ascending should be a boolean, not a string -> Option DQuick Check:
ascending must be True/False boolean [OK]
- Passing 'False' as string instead of boolean
- Thinking column name must be list always
- Assuming no error with wrong ascending type
df with columns 'city' and 'temperature'. You want to sort it first by 'city' alphabetically ascending, then by 'temperature' descending. Which code does this correctly?Solution
Step 1: Understand multi-column sorting
To sort by multiple columns, pass a list tobyand a list of booleans toascendingmatching each column.Step 2: Match sorting order to columns
We want'city'ascending (True) and'temperature'descending (False), soascending=[True, False].Final Answer:
df.sort_values(by=['city', 'temperature'], ascending=[True, False]) -> Option BQuick Check:
Multi-column sort uses list of booleans [OK]
- Using single boolean for multi-column ascending
- Swapping column order in by list
- Mixing ascending True/False order
Ready to prove it?
Pass the test and this pattern moves unknown
