📖 Scenario: Imagine you have a small store and you keep track of your sales data. You want to find sales information quickly and easily. Using indexing in pandas helps you do that.
🎯 Goal: You will create a sales data table, set an index to organize it by product names, and then use that index to find sales data quickly.
📋 What You'll Learn
Create a pandas DataFrame with sales data
Set the product names as the index of the DataFrame
Use the index to select sales data for a specific product
Print the selected sales data
💡 Why This Matters
🌍 Real World
Indexing helps you quickly find and organize data in tables, like looking up products in a store inventory.
💼 Career
Data scientists use indexing to efficiently access and analyze data, making their work faster and more accurate.
Progress0 / 4 steps
1
Create the sales data DataFrame
Import pandas as pd. Create a DataFrame called sales_data with these exact columns and values: 'Product' with values 'Apple', 'Banana', 'Cherry'; 'Quantity' with values 10, 20, 15; and 'Price' with values 0.5, 0.3, 0.2.
Pandas
Hint
Use pd.DataFrame and pass a dictionary with keys as column names and values as lists of data.
2
Set the product names as the index
Set the 'Product' column as the index of the sales_data DataFrame using set_index and save it back to sales_data.
Pandas
Hint
Use sales_data.set_index('Product') and assign it back to sales_data.
3
Select sales data for a specific product using the index
Use the index to select the row for 'Banana' from sales_data and save it in a variable called banana_data.
Pandas
Hint
Use sales_data.loc['Banana'] to get the row for Banana.
4
Print the selected sales data
Print the variable banana_data to display the sales information for Banana.
Pandas
Hint
Use print(banana_data) to show the sales data for Banana.
Practice
(1/5)
1. Why is indexing important in pandas DataFrames?
easy
A. It deletes duplicate rows.
B. It changes the data types of columns.
C. It sorts the DataFrame automatically.
D. It labels rows to find data quickly.
Solution
Step 1: Understand the role of indexing
Indexing assigns labels to rows, making it easier to locate and access data quickly.
Step 2: Compare with other options
Changing data types, sorting, or deleting duplicates are different operations unrelated to indexing.
Final Answer:
It labels rows to find data quickly. -> Option D
Quick Check:
Indexing = labeling rows [OK]
Hint: Indexing helps find rows fast by labeling them [OK]
Common Mistakes:
Confusing indexing with sorting
Thinking indexing changes data types
Assuming indexing deletes duplicates
2. Which of the following is the correct way to set the 'Name' column as the index of a DataFrame df?
easy
A. df.setIndex('Name')
B. df.index('Name')
C. df.set_index('Name')
D. df.index = 'Name'
Solution
Step 1: Recall the correct pandas method
The method to set a column as index is set_index, called as df.set_index('column_name').
Step 2: Check other options for syntax errors
df.index('Name') and df.setIndex('Name') are invalid methods. Assigning df.index = 'Name' sets index to a string, not a column.
Final Answer:
df.set_index('Name') -> Option C
Quick Check:
Use set_index('column') to set index [OK]
Hint: Use df.set_index('column') to set index [OK]
Common Mistakes:
Using wrong method names like setIndex
Trying to call df.index as a function
Assigning a string directly to df.index
3. Given the DataFrame df below:
Name Age Score
0 Alice 25 88
1 Bob 30 92
2 Carol 22 85
What is the output of df.loc[1, 'Score']?
medium
A. 30
B. 92
C. Bob
D. 85
Solution
Step 1: Understand loc usage
loc selects data by label. Here, row label 1 and column 'Score' is requested.
Step 2: Find value at row 1, column 'Score'
Row 1 corresponds to Bob, and his Score is 92.
Final Answer:
92 -> Option B
Quick Check:
df.loc[1, 'Score'] = 92 [OK]
Hint: loc uses row labels, so df.loc[1, 'Score'] = 92 [OK]
The DataFrame has columns 'A' and 'B' only; 'C' is missing.
Step 2: Understand set_index behavior
Trying to set index to a non-existent column raises a KeyError.
Final Answer:
Column 'C' does not exist, causing an error. -> Option A
Quick Check:
Missing column in set_index causes error [OK]
Hint: Check if column exists before set_index [OK]
Common Mistakes:
Assuming set_index works without error on missing columns
Thinking set_index can't be used on DataFrames
Ignoring error messages
5. You have a DataFrame df with columns 'City', 'Year', and 'Population'. You want to quickly select all rows for the city 'Paris' using indexing. Which steps correctly prepare and select the data?
hard
A. df.set_index('City', inplace=True)\ndf.loc['Paris']
B. df.set_index('Year', inplace=True)\ndf.loc['Paris']
C. df.set_index('Population', inplace=True)\ndf.loc['Paris']
D. df.loc[df['City'] == 'Paris']
Solution
Step 1: Set 'City' as the index
Setting 'City' as index allows direct row selection by city name.
Step 2: Use loc to select rows labeled 'Paris'
With 'City' as index, df.loc['Paris'] returns all rows for Paris efficiently.
Final Answer:
df.set_index('City', inplace=True)\ndf.loc['Paris'] -> Option A
Quick Check:
Index by City, then loc['Paris'] selects rows [OK]
Hint: Set index to City, then use loc['Paris'] to select [OK]
Common Mistakes:
Setting wrong column as index
Using loc with non-index column
Not setting index before loc selection
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.