0
0
Pandasdata~15 mins

Setting a column as index in Pandas - Mini Project: Build & Apply

Choose your learning style9 modes available
Setting a Column as Index in pandas DataFrame
📖 Scenario: You work in a small bookstore. You have a list of books with their details in a table. You want to organize this table so that each book's unique ID is used to find its details quickly.
🎯 Goal: Create a pandas DataFrame with book details, then set the BookID column as the index to organize the data better.
📋 What You'll Learn
Create a pandas DataFrame named books with columns BookID, Title, and Author with the exact data provided.
Create a variable index_column and set it to the string 'BookID'.
Use the set_index() method on the books DataFrame with index_column to set the index.
Assign the result back to the books variable.
💡 Why This Matters
🌍 Real World
Setting a column as an index helps quickly find and organize data, like looking up books by their unique ID in a bookstore system.
💼 Career
Data analysts and scientists often set meaningful indexes in data tables to improve data access speed and clarity.
Progress0 / 4 steps
1
Create the books DataFrame
Create a pandas DataFrame called books with these exact columns and data: BookID with values 101, 102, 103; Title with values 'Python Basics', 'Data Science', 'Machine Learning'; Author with values 'Alice', 'Bob', 'Charlie'.
Pandas
Need a hint?

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

2
Create the index column variable
Create a variable called index_column and set it to the string 'BookID'.
Pandas
Need a hint?

Just assign the string 'BookID' to the variable index_column.

3
Set the index using set_index()
Use the set_index() method on the books DataFrame with the variable index_column to set the index. Assign the result back to the books variable.
Pandas
Need a hint?

Use books.set_index(index_column) and assign it back to books.

4
Verify the index is set
Add a line to check that the index of the books DataFrame is set to BookID by accessing books.index.name and assigning it to a variable called index_name.
Pandas
Need a hint?

Use books.index.name to get the index column name and assign it to index_name.