Selects the second row by position (counting starts at 0).
Pandas
df.iloc[1]
Sample Program
This program shows how setting the 'City' column as the index helps us select data by city name easily.
Pandas
import pandas as pd
# Create a simple table
data = {'City': ['Paris', 'London', 'Berlin'], 'Population': [2148000, 8982000, 3769000]}
df = pd.DataFrame(data)
# Set 'City' as the index
df_indexed = df.set_index('City')
# Select population of London using index label
london_pop = df_indexed.loc['London', 'Population']
print('Table with City as index:')
print(df_indexed)
print(f"Population of London: {london_pop}")
OutputSuccess
Important Notes
Indexes can be simple (one column) or multi-level (multiple columns).
Using meaningful indexes makes data easier to read and faster to access.
Remember to reset the index if you want to go back to default numbering.
Summary
Indexing labels rows to help find data quickly.
Use set_index to choose which column labels rows.
loc and iloc help select data by label or position.
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.