0
0
Pandasdata~15 mins

iloc for position-based selection in Pandas - Mini Project: Build & Apply

Choose your learning style9 modes available
Using iloc for Position-Based Selection in pandas
📖 Scenario: You work in a small bookstore. You have a list of books with their prices and stock counts. You want to select specific rows and columns by their position to analyze the data.
🎯 Goal: Learn how to use iloc in pandas to select rows and columns by their integer position.
📋 What You'll Learn
Create a pandas DataFrame with book data
Create a variable for row and column positions
Use iloc to select data by position
Print the selected data
💡 Why This Matters
🌍 Real World
Selecting data by position is useful when you do not know the exact labels or want to quickly access data by order, such as recent sales or top products.
💼 Career
Data analysts and scientists often use iloc to slice and dice data efficiently when working with large datasets.
Progress0 / 4 steps
1
Create the book DataFrame
Import pandas as pd and create a DataFrame called books with these exact data: columns 'Title', 'Price', 'Stock' and rows: 'Python Basics' priced 25 with 10 in stock, 'Data Science' priced 40 with 5 in stock, and 'Machine Learning' priced 50 with 2 in stock.
Pandas
Need a hint?

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

2
Set row and column positions
Create two variables: row_pos and col_pos. Set row_pos to 1 to select the second row, and col_pos to 2 to select the third column.
Pandas
Need a hint?

Remember, Python counting starts at 0, so 1 means the second row.

3
Select data using iloc
Use iloc on the books DataFrame with row_pos and col_pos to select the value at that row and column position. Store the result in a variable called selected_value.
Pandas
Need a hint?

Use books.iloc[row_pos, col_pos] to select by position.

4
Print the selected value
Print the variable selected_value to display the stock count of the second book.
Pandas
Need a hint?

Use print(selected_value) to show the result.