Slicing and Selecting Using .iloc[]

Slicing Dataframe

cereal = pd.read_csv('data/cereal.csv')
cereal.head(10)
name mfr type calories ... shelf weight cups rating
0 100% Bran N Cold 70 ... 3 1.00 0.33 68.402973
1 100% Natural Bran Q Cold 120 ... 3 1.00 1.00 33.983679
2 All-Bran K Cold 70 ... 3 1.00 0.33 59.425505
3 All-Bran with Extra Fiber K Cold 50 ... 3 1.00 0.50 93.704912
4 Almond Delight R Cold 110 ... 3 1.00 0.75 34.384843
5 Apple Cinnamon Cheerios G Cold 110 ... 1 1.00 0.75 29.509541
6 Apple Jacks K Cold 110 ... 2 1.00 1.00 33.174094
7 Basic 4 G Cold 130 ... 3 1.33 0.75 37.038562
8 Bran Chex R Cold 90 ... 1 1.00 0.67 49.120253
9 Bran Flakes P Cold 90 ... 3 1.00 0.67 53.313813

10 rows × 16 columns

cereal.loc[2:5]
name mfr type calories ... shelf weight cups rating
2 All-Bran K Cold 70 ... 3 1.0 0.33 59.425505
3 All-Bran with Extra Fiber K Cold 50 ... 3 1.0 0.50 93.704912
4 Almond Delight R Cold 110 ... 3 1.0 0.75 34.384843
5 Apple Cinnamon Cheerios G Cold 110 ... 1 1.0 0.75 29.509541

4 rows × 16 columns


cereal.iloc[2:5]
name mfr type calories ... shelf weight cups rating
2 All-Bran K Cold 70 ... 3 1.0 0.33 59.425505
3 All-Bran with Extra Fiber K Cold 50 ... 3 1.0 0.50 93.704912
4 Almond Delight R Cold 110 ... 3 1.0 0.75 34.384843

3 rows × 16 columns

cereal.iloc[2:6]
name mfr type calories ... shelf weight cups rating
2 All-Bran K Cold 70 ... 3 1.0 0.33 59.425505
3 All-Bran with Extra Fiber K Cold 50 ... 3 1.0 0.50 93.704912
4 Almond Delight R Cold 110 ... 3 1.0 0.75 34.384843
5 Apple Cinnamon Cheerios G Cold 110 ... 1 1.0 0.75 29.509541

4 rows × 16 columns

name mfr type calories ... shelf weight cups rating
0 100% Bran N Cold 70 ... 3 1.0 0.33 68.402973
1 100% Natural Bran Q Cold 120 ... 3 1.0 1.00 33.983679
2 All-Bran K Cold 70 ... 3 1.0 0.33 59.425505
3 All-Bran with Extra Fiber K Cold 50 ... 3 1.0 0.50 93.704912
4 Almond Delight R Cold 110 ... 3 1.0 0.75 34.384843

5 rows × 16 columns

cereal.iloc[:, 0:6]
name mfr type calories protein fat
0 100% Bran N Cold 70 4 1
1 100% Natural Bran Q Cold 120 3 5
2 All-Bran K Cold 70 4 1
3 All-Bran with Extra Fiber K Cold 50 4 0
4 Almond Delight R Cold 110 2 2
... ... ... ... ... ... ...
72 Triples G Cold 110 2 1
73 Trix G Cold 110 1 1
74 Wheat Chex R Cold 100 3 1
75 Wheaties G Cold 100 3 1
76 Wheaties Honey Gold G Cold 110 2 1

77 rows × 6 columns

Let’s say we want the rows All-Bran to Apple Cinnamon Cheerios and name to fat.

Rows

Lower Bound: All-Bran located at position 2.
Upper Bound:Apple Cinnamon Cheerios is located at position 5.

Columns

Lower Bound: name is located at position 0.
Upper Bound:fat is located at position 5.


So the code we have to use to do this is the following:

cereal.iloc[2:6, 0:6]
name mfr type calories protein fat
2 All-Bran K Cold 70 4 1
3 All-Bran with Extra Fiber K Cold 50 4 0
4 Almond Delight R Cold 110 2 2
5 Apple Cinnamon Cheerios G Cold 110 2 2
cereal.head(3)
name mfr type calories ... shelf weight cups rating
0 100% Bran N Cold 70 ... 3 1.0 0.33 68.402973
1 100% Natural Bran Q Cold 120 ... 3 1.0 1.00 33.983679
2 All-Bran K Cold 70 ... 3 1.0 0.33 59.425505

3 rows × 16 columns


cereal.iloc[0:3]
name mfr type calories ... shelf weight cups rating
0 100% Bran N Cold 70 ... 3 1.0 0.33 68.402973
1 100% Natural Bran Q Cold 120 ... 3 1.0 1.00 33.983679
2 All-Bran K Cold 70 ... 3 1.0 0.33 59.425505

3 rows × 16 columns


cereal.iloc[:3]
name mfr type calories ... shelf weight cups rating
0 100% Bran N Cold 70 ... 3 1.0 0.33 68.402973
1 100% Natural Bran Q Cold 120 ... 3 1.0 1.00 33.983679
2 All-Bran K Cold 70 ... 3 1.0 0.33 59.425505

3 rows × 16 columns

cereal.iloc[74:77]
name mfr type calories ... shelf weight cups rating
74 Wheat Chex R Cold 100 ... 1 1.0 0.67 49.787445
75 Wheaties G Cold 100 ... 1 1.0 1.00 51.592193
76 Wheaties Honey Gold G Cold 110 ... 1 1.0 0.75 36.187559

3 rows × 16 columns


cereal.iloc[-3:]
name mfr type calories ... shelf weight cups rating
74 Wheat Chex R Cold 100 ... 1 1.0 0.67 49.787445
75 Wheaties G Cold 100 ... 1 1.0 1.00 51.592193
76 Wheaties Honey Gold G Cold 110 ... 1 1.0 0.75 36.187559

3 rows × 16 columns

Selecting with .iloc[]

cereal.head(10)
name mfr type calories ... shelf weight cups rating
0 100% Bran N Cold 70 ... 3 1.00 0.33 68.402973
1 100% Natural Bran Q Cold 120 ... 3 1.00 1.00 33.983679
2 All-Bran K Cold 70 ... 3 1.00 0.33 59.425505
3 All-Bran with Extra Fiber K Cold 50 ... 3 1.00 0.50 93.704912
4 Almond Delight R Cold 110 ... 3 1.00 0.75 34.384843
5 Apple Cinnamon Cheerios G Cold 110 ... 1 1.00 0.75 29.509541
6 Apple Jacks K Cold 110 ... 2 1.00 1.00 33.174094
7 Basic 4 G Cold 130 ... 3 1.33 0.75 37.038562
8 Bran Chex R Cold 90 ... 1 1.00 0.67 49.120253
9 Bran Flakes P Cold 90 ... 3 1.00 0.67 53.313813

10 rows × 16 columns

Row Row Position
Almond Delight Position 4
Basic 4 Position 7
Apple Jacks Position 6
Columns Column Position
name Position 0
calories Position 3
fat Position 5
type Position 2
Row Row Position
Almond Delight Position 4
Basic 4 Position 7
Apple Jacks Position 6
Columns Column Position
name Position 0
calories Position 3
fat Position 5
type Position 2










cereal.iloc[[4, 7, 6], [0, 3, 5, 2]]
name calories fat type
4 Almond Delight 110 2 Cold
7 Basic 4 130 2 Cold
6 Apple Jacks 110 0 Cold

Nice work! Let’s apply what we learned!