0
0
Pandasdata~5 mins

iloc for position-based selection in Pandas - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does iloc do in pandas?

iloc selects rows and columns by their integer position in the DataFrame, not by label.

Click to reveal answer
beginner
How do you select the first row of a DataFrame using iloc?

Use df.iloc[0] to select the first row by position.

Click to reveal answer
intermediate
How to select the first 3 rows and first 2 columns using iloc?

Use df.iloc[0:3, 0:2] to select rows 0 to 2 and columns 0 to 1.

Click to reveal answer
intermediate
What happens if you use a negative index with iloc?

Negative indices count from the end. For example, df.iloc[-1] selects the last row.

Click to reveal answer
intermediate
Can iloc accept lists or arrays for selection?

Yes, you can pass lists or arrays of integer positions to select specific rows or columns, e.g., df.iloc[[0,2,4]].

Click to reveal answer
What does df.iloc[2, 3] select?
ARows 2 to 3
BRow with label 2 and column with label 3
CSecond row and third column by position
DThird row and fourth column by position
How to select all rows but only the first column using iloc?
Adf.iloc[:, 0]
Bdf.iloc[0, :]
Cdf.iloc[0]
Ddf.iloc[:, 1]
What will df.iloc[1:4] return?
ARows at positions 1, 2, and 3
BRows at positions 1 to 4 inclusive
CRows with labels 1 to 4
DColumns 1 to 4
Which of these is NOT a valid iloc indexer?
AAn integer
BA string label
CA boolean mask
DA list of integers
What does df.iloc[-2] select?
ASecond row
BSecond column
CSecond last row
DLast row
Explain how to use iloc to select specific rows and columns by position in a DataFrame.
Think about selecting by counting rows and columns from zero.
You got /4 concepts.
    Describe the difference between loc and iloc in pandas.
    One uses names, the other uses counting.
    You got /4 concepts.