📖 Scenario: You work in a small bookstore. You have a list of books with their sales numbers. You want to find out which books sold the most and which sold the least.
🎯 Goal: You will create a pandas DataFrame with book sales data, then sort the books by their sales numbers to see the best and worst sellers.
📋 What You'll Learn
Create a pandas DataFrame with book titles and their sales numbers
Create a variable to decide sorting order
Sort the DataFrame by sales numbers using the sorting order variable
Print the sorted DataFrame
💡 Why This Matters
🌍 Real World
Sorting data by values is common in sales reports, rankings, and any list where order matters.
💼 Career
Data analysts and scientists often sort data to find top performers or trends quickly.
Progress0 / 4 steps
1
Create the book sales DataFrame
Import pandas as pd. Create a DataFrame called book_sales with two columns: 'Book' and 'Sales'. Use these exact entries: 'The Alchemist' with 150, '1984' with 200, 'The Catcher in the Rye' with 120, 'To Kill a Mockingbird' with 180, and 'Moby Dick' with 90.
Pandas
Hint
Use pd.DataFrame with a dictionary containing the book names and sales numbers.
2
Set the sorting order
Create a variable called ascending_order and set it to False to sort from highest to lowest sales.
Pandas
Hint
Set ascending_order to False to sort from highest to lowest.
3
Sort the DataFrame by sales
Create a new DataFrame called sorted_sales by sorting book_sales by the 'Sales' column using the ascending_order variable.
Pandas
Hint
Use book_sales.sort_values(by='Sales', ascending=ascending_order) to sort.
4
Print the sorted DataFrame
Print the sorted_sales DataFrame to see the books sorted by sales.
Pandas
Hint
Use print(sorted_sales) to show the sorted DataFrame.
Practice
(1/5)
1. What does the sort_values() function do in pandas?
easy
A. It filters rows based on a condition.
B. It groups data by a column.
C. It sorts the DataFrame by one or more columns.
D. It merges two DataFrames.
Solution
Step 1: Understand the function purpose
The sort_values() function is designed to reorder rows in a DataFrame based on the values in one or more columns.
Step 2: Compare with other functions
Filtering, grouping, and merging are done by different pandas functions like filter(), groupby(), and merge().
Final Answer:
It sorts the DataFrame by one or more columns. -> Option C
Quick Check:
sort_values() = reorder rows by column values [OK]
Hint: Remember: sort_values orders rows by column data [OK]
Common Mistakes:
Confusing sorting with filtering
Thinking it groups data
Mixing it up with merging DataFrames
2. Which of the following is the correct syntax to sort a DataFrame df by the column 'age' in descending order?
easy
A. df.sort_values(by='age', ascending=False)
B. df.sort_values('age', descending=True)
C. df.sort('age', ascending=False)
D. df.sort_values('age', ascending=True)
Solution
Step 1: Identify correct parameter names
The correct parameter to specify the column is by='age', and to sort descending, use ascending=False.
Step 2: Check syntax correctness
df.sort_values(by='age', ascending=False) uses the correct function name sort_values and correct parameters. df.sort_values('age', ascending=True) sorts ascending, C uses a wrong function sort, and B uses a wrong parameter descending=True.
Final Answer:
df.sort_values(by='age', ascending=False) -> Option A
A. The column name 'age' must be passed as a list.
B. The ascending parameter should be a boolean, not a string.
C. sort_values does not accept the ascending parameter.
D. The function should be sort_by_values, not sort_values.
Solution
Step 1: Check parameter types
The ascending parameter expects a boolean value True or False, not a string.
Step 2: Validate other parts
The column name can be a string or list, sort_values is the correct function, and it accepts ascending.
Final Answer:
The ascending parameter should be a boolean, not a string. -> Option B
Quick Check:
ascending must be True/False boolean, not 'False' string [OK]
Hint: Use True/False without quotes for ascending parameter [OK]
Common Mistakes:
Passing 'False' as a string instead of boolean
Thinking column name must be a list
Using wrong function name
5. You have a DataFrame df with columns 'city' and 'temperature'. You want to sort it first by 'city' alphabetically ascending, then by 'temperature' descending within each city. Which code correctly does this?
hard
A. df.sort_values(by=['city', 'temperature'], ascending=[True, False])
B. df.sort_values(by=['temperature', 'city'], ascending=[False, True])
C. df.sort_values(by=['city', 'temperature'], ascending=False)
D. df.sort_values(['city', 'temperature'], ascending=[False, True])
Solution
Step 1: Understand multi-column sorting
To sort by multiple columns, pass a list to by and a list of booleans to ascending matching each column.
Step 2: Match sorting order requirements
We want 'city' ascending (True) and 'temperature' descending (False), so ascending=[True, False].
Step 3: Check options
df.sort_values(by=['city', 'temperature'], ascending=[True, False]) matches the correct order and ascending list. df.sort_values(by=['temperature', 'city'], ascending=[False, True]) reverses columns. df.sort_values(by=['city', 'temperature'], ascending=False) uses a single boolean for two columns. df.sort_values(['city', 'temperature'], ascending=[False, True]) has wrong ascending order.
Final Answer:
df.sort_values(by=['city', 'temperature'], ascending=[True, False]) -> Option A
Quick Check:
Multi-column sort uses lists for by and ascending [OK]
Hint: Use lists for by and ascending to sort multiple columns [OK]
Common Mistakes:
Using single boolean for multiple columns
Mixing column order in by list
Wrong ascending order list
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.