0
0
Pandasdata~15 mins

sort_values() by single column in Pandas - Mini Project: Build & Apply

Choose your learning style9 modes available
Sorting Data with pandas sort_values() by Single Column
📖 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 to find the cheapest and most expensive ones easily.
🎯 Goal: Build a small program that creates a DataFrame of books and prices, then sorts the books by their price using pandas sort_values() method.
📋 What You'll Learn
Create a pandas DataFrame with book titles and prices
Create a variable to hold the column name to sort by
Use sort_values() to sort the DataFrame by the price column
Print the sorted DataFrame
💡 Why This Matters
🌍 Real World
Sorting data by a specific column is a common task in data analysis to organize and understand data better.
💼 Career
Data scientists and analysts often sort data to prepare reports, find trends, 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.99, '1984' priced at 8.99, 'Python 101' priced at 15.50, 'Data Science' priced at 12.75.
Pandas
Need a hint?

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

2
Set the column to sort by
Create a variable called sort_column and set it to the string 'Price' to specify the column to sort the DataFrame by.
Pandas
Need a hint?

Just assign the string 'Price' to the variable sort_column.

3
Sort the DataFrame by the price column
Create a new DataFrame called sorted_books by sorting the books DataFrame using the sort_values() method with the column name stored in sort_column.
Pandas
Need a hint?

Use books.sort_values(by=sort_column) to sort by the column stored in sort_column.

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.