Chapter 1 ยท Section 3 practice
Selection and Index Alignment
These questions review the examples in the companion slides. Choose an answer, then check the explanation.
Question 1
How many rows do aep.loc[โ2017-01-01 01:00โ:โ2017-01-01 05:00โ] and aep.iloc[0:5] return?
- 4 and 5
- 5 and 5
- 5 and 4
- 4 and 4
Question 2
For jan = sp500.loc[โ2020โ], do jan.loc[โ2020-01-06โ:โ2020-01-08โ] and jan.iloc[2:5] return the same rows?
- Yes โ Jan 6, 7, 8 both times, but for opposite slicing rules
- No โ
.locgives 2 rows,.ilocgives 3 - No โ
.ilocincludes Jan 9 - They raise โ you cannot mix index value and position
Question 3
r.loc[r > 0.02 & r < 0.05] โฆ
- Works โ same as the parenthesised version
- Returns an empty Series
- Raises
TypeErrorโ&binds tighter than> - Returns the days above 2 % only
Question 4
price.sample(500, replace=True, random_state=0) โฆ
- Always returns the first 500 rows
- Returns 500 distinct rows
- May contain the same product twice
- Raises unless
fracis given