0
0
Data Analysis Pythondata~30 mins

Selecting rows (loc, iloc) in Data Analysis Python - Mini Project: Build & Apply

Choose your learning style9 modes available
Selecting Rows with loc and iloc in Pandas
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Use two print() statements, one for each selected row.