0
0
Data Analysis Pythondata~5 mins

Selecting rows (loc, iloc) in Data Analysis Python - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does loc do when selecting rows in a DataFrame?

loc selects rows by their labels or index names. It includes the last label in slicing.

Click to reveal answer
beginner
How does iloc differ from loc when selecting rows?

iloc selects rows by their integer position, starting at 0. It excludes the last index in slicing.

Click to reveal answer
beginner
What will df.loc[2:4] return if the DataFrame index is [0, 1, 2, 3, 4, 5]?

Rows with index labels 2, 3, and 4 inclusive.

Click to reveal answer
beginner
What will df.iloc[2:4] return if the DataFrame index is [0, 1, 2, 3, 4, 5]?

Rows at integer positions 2 and 3 (index labels 2 and 3), excluding position 4.

Click to reveal answer
intermediate
Can loc select rows using boolean conditions?

Yes, loc can select rows by passing a boolean mask (True/False values) to filter rows.

Click to reveal answer
Which method selects rows by integer position in a DataFrame?
Aloc
Bfilter_rows
Cselect_rows
Diloc
What does df.loc[1:3] include when selecting rows?
ARows at integer positions 1 and 2 only
BRows with index labels 1, 2, and 3
CRows with index labels 1 and 2 only
DRows at integer positions 1, 2, and 3
If a DataFrame index is ['a', 'b', 'c', 'd'], what does df.iloc[1:3] select?
ARows with labels 'c' and 'd'
BRows with labels 'a' and 'b'
CRows with labels 'b' and 'c'
DRows with labels 'b', 'c', and 'd'
Which of these is true about loc slicing?
AIt includes the last label in the slice
BIt excludes the last label in the slice
CIt only works with integer indexes
DIt cannot use boolean masks
How can you select rows where a column 'age' is greater than 30 using loc?
Adf.loc[df['age'] > 30]
Bdf.iloc[df['age'] > 30]
Cdf.loc['age' > 30]
Ddf.iloc['age' > 30]
Explain the difference between loc and iloc when selecting rows in a DataFrame.
Think about how you pick rows by name versus by number.
You got /4 concepts.
    Describe how to select rows based on a condition using loc.
    Use True/False values to pick rows.
    You got /4 concepts.