0
0
Pandasdata~30 mins

Sorting by values in Pandas - Mini Project: Build & Apply

Choose your learning style9 modes available
Sorting by values
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Use print(sorted_sales) to show the sorted DataFrame.