0
0
Pandasdata~30 mins

Why sorting and ranking matter in Pandas - See It in Action

Choose your learning style9 modes available
Why sorting and ranking matter
📖 Scenario: Imagine 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. Sorting and ranking the sales data will help you understand this quickly.
🎯 Goal: You will create a small dataset of books and their sales, then sort and rank the books by their sales numbers to see which are the top sellers and which are not.
📋 What You'll Learn
Create a pandas DataFrame with book titles and their sales numbers
Create a variable to hold the minimum sales threshold
Sort the DataFrame by sales in descending order
Add a ranking column based on sales
Print the sorted and ranked DataFrame
💡 Why This Matters
🌍 Real World
Sorting and ranking help businesses quickly identify top products, best sellers, or priority items from their data.
💼 Career
Data analysts and scientists often sort and rank data to generate reports, insights, and make data-driven decisions.
Progress0 / 4 steps
1
Create the book sales data
Create a pandas DataFrame called books with two columns: 'Title' and 'Sales'. Use these exact entries: 'The Alchemist' with sales 150, '1984' with sales 200, 'The Hobbit' with sales 180, 'Fahrenheit 451' with sales 120, and 'To Kill a Mockingbird' with sales 170.
Pandas
Need a hint?

Use pd.DataFrame with a dictionary where keys are column names and values are lists of data.

2
Set the minimum sales threshold
Create a variable called min_sales and set it to 130. This will help us filter books with sales above this number later.
Pandas
Need a hint?

Just assign the number 130 to the variable min_sales.

3
Sort and rank the books by sales
Sort the books DataFrame by the 'Sales' column in descending order and save it back to books. Then add a new column called 'Rank' that ranks the books by sales, with 1 being the highest sales.
Pandas
Need a hint?

Use sort_values with ascending=False to sort from highest to lowest. Use rank(method='min', ascending=False) to assign ranks.

4
Print the sorted and ranked books
Print the books DataFrame to see the sorted and ranked list of books by sales.
Pandas
Need a hint?

Use print(books) to show the DataFrame.