Slicing with Pandas Using .loc[]

Cereal Data

import pandas as pd
  
cereal = pd.read_csv('data/cereal.csv')
cereal.head()
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


Attribution:
80 Cereals” (c) by Chris Crawford is licensed under Creative Commons Attribution-ShareAlike 3.0 Unported

cereal.head(15)
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 Cap'n'Crunch Q Cold 120 ... 2 1.00 0.75 18.042851
11 Cheerios G Cold 110 ... 1 1.00 1.25 50.764999
12 Cinnamon Toast Crunch G Cold 120 ... 2 1.00 0.75 19.823573
13 Clusters G Cold 110 ... 3 1.00 0.50 40.400208
14 Cocoa Puffs G Cold 110 ... 2 1.00 1.00 22.736446

15 rows × 16 columns

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 Cap'n'Crunch Q Cold 120 ... 2 1.00 0.75 18.042851
11 Cheerios G Cold 110 ... 1 1.00 1.25 50.764999
12 Cinnamon Toast Crunch G Cold 120 ... 2 1.00 0.75 19.823573
13 Clusters G Cold 110 ... 3 1.00 0.50 40.400208
14 Cocoa Puffs G Cold 110 ... 2 1.00 1.00 22.736446

15 rows × 16 columns

cereal.loc[5:10]
name mfr type calories ... shelf weight cups rating
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 Cap'n'Crunch Q Cold 120 ... 2 1.00 0.75 18.042851

6 rows × 16 columns

name mfr type calories ... shelf weight cups rating
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 Cap'n'Crunch Q Cold 120 ... 2 1.00 0.75 18.042851

6 rows × 16 columns

cereal.loc[5:10, 'calories':'fiber']
calories protein fat sodium fiber
5 110 2 2 180 1.5
6 110 2 0 125 1.0
7 130 3 2 210 2.0
8 90 2 1 200 4.0
9 90 3 0 210 5.0
10 120 1 2 220 0.0


The general format to slice both rows and columns together looks like this:

cereal.loc['row name start':'row name end', 'column name start':'column name end']
cereal.loc[:6]
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 Apple Cinnamon Cheerios G Cold 110 ... 1 1.0 0.75 29.509541
6 Apple Jacks K Cold 110 ... 2 1.0 1.00 33.174094

7 rows × 16 columns


cereal.loc[:6, 'sugars':]
sugars potass vitamins shelf weight cups rating
0 6 280 25 3 1.0 0.33 68.402973
1 8 135 0 3 1.0 1.00 33.983679
2 5 320 25 3 1.0 0.33 59.425505
3 0 330 25 3 1.0 0.50 93.704912
4 8 1 25 3 1.0 0.75 34.384843
5 10 70 25 1 1.0 0.75 29.509541
6 14 30 25 2 1.0 1.00 33.174094

Let’s try it out!