0
0
Pandasdata~20 mins

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

Choose your learning style9 modes available
Sorting DataFrames by Index with pandas
📖 Scenario: You work in a small bookstore. You have a list of books with their prices stored in a table. You want to organize this list by the book titles to find books quickly.
🎯 Goal: Build a pandas DataFrame with book titles as the index and prices as the values. Then sort the DataFrame by its index (book titles) in ascending order.
📋 What You'll Learn
Create a pandas DataFrame called books with the given data and set the book titles as the index.
Create a variable called sorted_books that stores the DataFrame sorted by its index in ascending order.
💡 Why This Matters
🌍 Real World
Sorting data by index helps organize and quickly find information in tables, like sorting books by title in a bookstore inventory.
💼 Career
Data analysts and database managers often sort data by index or keys to improve data retrieval and reporting.
Progress0 / 4 steps
1
Create the books DataFrame with index
Import pandas as pd. Create a DataFrame called books with these entries: titles 'Python Basics', 'Data Science 101', 'Machine Learning', and prices 25, 40, 55 respectively. Set the book titles as the index of the DataFrame.
Pandas
Need a hint?

Use pd.DataFrame with a dictionary for prices and set index to the list of book titles.

2
Set 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 assign True to the variable ascending_order.

3
Sort the DataFrame by index
Create a new DataFrame called sorted_books by sorting books by its index in ascending order using the variable ascending_order.
Pandas
Need a hint?

Use books.sort_index(ascending=ascending_order) to sort by index.

4
Complete the sorting project
Ensure the variable sorted_books contains the DataFrame sorted by index in ascending order. This completes the sorting by index task.
Pandas
Need a hint?

No extra code needed here. Just confirm the sorting is done.