pandas Series and DataFrames: making them, selecting with loc and iloc, filtering, changing and saving them.
| Labels a to f | s.loc['b':'d'] → b, c, d; s.iloc[1:3] → b, c |
| Some columns | df[['Name', 'Maths']] (two pairs of brackets) |
| shape (r, c), size r × c | add a column: c + 1; drop a row: r − 1; drop a column: c − 1 |
Series s:
a | 38
b | 96
c | 75
d | 29
e | 61
f | 56
Write the output of each statement, and say why they differ.
(a) print(s.loc['b':'f'])
(b) print(s.iloc[1:5])
Working: loc slices by label (end included); iloc by position (end left out)
Answer: (a)
b | 96
c | 75
d | 29
e | 61
f | 56
(b)
b | 96
c | 75
d | 29
e | 61
loc includes the end label f; iloc stops before position 5.