📖 Scenario: You work in a small bookstore. You have a list of books with their details. You want to find specific books by their position or by their label.
🎯 Goal: Learn how to select rows from a table using loc and iloc in pandas.
📋 What You'll Learn
Create a pandas DataFrame with book data
Create variables to select specific rows
Use loc to select rows by label
Use iloc to select rows by position
Print the selected rows
💡 Why This Matters
🌍 Real World
Selecting rows by label or position is common when working with data tables, like filtering customer records or finding specific entries in a dataset.
💼 Career
Data analysts and scientists often need to extract specific rows from large datasets to analyze or visualize data effectively.
Progress0 / 4 steps
1
Create the books DataFrame
Import pandas as pd and create a DataFrame called books with these exact entries: index labels 101, 102, 103, 104; columns 'Title', 'Author', 'Year'; and rows: ['The Alchemist', 'Paulo Coelho', 1988], ['1984', 'George Orwell', 1949], ['To Kill a Mockingbird', 'Harper Lee', 1960], ['The Great Gatsby', 'F. Scott Fitzgerald', 1925].
Data Analysis Python
Hint
Use pd.DataFrame with a dictionary for columns and index for row labels.
2
Create variables for row selection
Create a variable called label_to_select and set it to 102. Create another variable called position_to_select and set it to 2.
Data Analysis Python
Hint
Just assign the numbers 102 and 2 to the variables.
3
Select rows using loc and iloc
Use loc on books with label_to_select to select the row by label and save it in row_by_label. Use iloc on books with position_to_select to select the row by position and save it in row_by_position.
Data Analysis Python
Hint
Use books.loc[label_to_select] and books.iloc[position_to_select].
4
Print the selected rows
Print row_by_label and then print row_by_position to show the selected rows.
Data Analysis Python
Hint
Use two print() statements, one for each selected row.
Practice
(1/5)
1. Which of the following statements correctly describes the difference between loc and iloc in pandas?
easy
A. loc selects rows by label, iloc selects rows by position.
B. loc selects columns by position, iloc selects rows by label.
C. loc selects rows by position, iloc selects columns by label.
D. loc and iloc both select rows by position.
Solution
Step 1: Understand loc usage
loc selects rows using the row labels or names, not their numeric position.
Step 2: Understand iloc usage
iloc selects rows based on their integer position, starting at zero.
Final Answer:
loc selects rows by label, iloc selects rows by position. -> Option A
Quick Check:
loc = label, iloc = position [OK]
Hint: Remember: loc = label, iloc = integer position [OK]
Common Mistakes:
Confusing label with position
Using iloc with labels
Assuming loc uses integer positions
2. Which of the following is the correct syntax to select the row with label '2023-01-01' from a DataFrame df using loc?
easy
A. df.loc[2023-01-01]
B. df.iloc[2023-01-01]
C. df.iloc['2023-01-01']
D. df.loc['2023-01-01']
Solution
Step 1: Identify correct method for label selection
To select by label, use loc with the label as a string if it is a string label.
Step 2: Check syntax for string label
The label '2023-01-01' must be in quotes inside loc brackets.
Final Answer:
df.loc['2023-01-01'] -> Option D
Quick Check:
Use quotes for string labels with loc [OK]
Hint: Use quotes around string labels inside loc brackets [OK]