0
0
Pandasdata~15 mins

Ascending and descending order in Pandas - Mini Project: Build & Apply

Choose your learning style9 modes available
Sorting Data in Ascending and Descending Order with pandas
📖 Scenario: You work in a small bookstore. You have a list of books with their prices. You want to see the books sorted by price from cheapest to most expensive, and also from most expensive to cheapest.
🎯 Goal: Learn how to sort data in ascending and descending order using pandas DataFrame.
📋 What You'll Learn
Create a pandas DataFrame with book titles and prices
Create a variable to control sorting order
Sort the DataFrame by price in ascending or descending order based on the variable
Print the sorted DataFrame
💡 Why This Matters
🌍 Real World
Sorting data is common in stores to show products from cheapest to most expensive or vice versa, helping customers make choices.
💼 Career
Data scientists often sort data to analyze trends, prepare reports, or clean data before deeper analysis.
Progress0 / 4 steps
1
Create the books DataFrame
Create a pandas DataFrame called books with two columns: 'Title' and 'Price'. Use these exact entries: 'The Alchemist' priced at 10, '1984' priced at 15, 'Python 101' priced at 20, 'Data Science' priced at 25.
Pandas
Need a hint?

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

2
Set the sorting order variable
Create a variable called ascending_order and set it to True to indicate sorting in ascending order.
Pandas
Need a hint?

Just create a variable named ascending_order and assign True to it.

3
Sort the DataFrame by price
Create a new DataFrame called sorted_books by sorting books by the 'Price' column. Use the ascending_order variable to decide if sorting is ascending or descending.
Pandas
Need a hint?

Use books.sort_values(by='Price', ascending=ascending_order) to sort.

4
Print the sorted DataFrame
Print the sorted_books DataFrame to see the books sorted by price.
Pandas
Need a hint?

Use print(sorted_books) to display the sorted DataFrame.