DataFrame Indexing: .loc[] vs .iloc[]


The loc and iloc functions are commonly used to select certain groups of rows (and columns) of a pandas DataFrame.

To explore these two functions and their differences, we'll use a DataFrame of 7 drinks with different features and nutrition facts:

import pandas as pd

# Create a DataFrame with "carbonated?", "temperature", "sugar(tsp.)", and "calories" columns:
df = pd.DataFrame([
  {'drink': 'soda', 'carbonated?': True, 'temperature': 'cold', 'sugar(tsp)': 10.5, 'calories': 150},
  {'drink': 'coffee', 'carbonated?': False, 'temperature': 'hot', 'sugar(tsp)': 3, 'calories': 31}, 
  {'drink': 'smoothie', 'carbonated?': False, 'temperature': 'cold', 'sugar(tsp)': 6, 'calories': 85}, 
  {'drink': 'water', 'carbonated?': False, 'temperature': 'cold', 'sugar(tsp)': 0, 'calories': 0},
  {'drink': 'tea', 'carbonated?': False, 'temperature': 'hot', 'sugar(tsp)': 2, 'calories': 43}, 
  {'drink': 'lemonade', 'carbonated?': False, 'temperature': 'cold', 'sugar(tsp)': 9.5, 'calories': 125},
  {'drink': 'slushy', 'carbonated?': False, 'temperature': 'cold', 'sugar(tsp)': 8, 'calories': 99},
])

# Set the row label indexes to the "drink" column:
df.set_index('drink', inplace = True)
df
carbonated?temperaturesugar(tsp)calories
drink
sodaTruecold10.5150
coffeeFalsehot3.031
smoothieFalsecold6.085
waterFalsecold0.00
teaFalsehot2.043
lemonadeFalsecold9.5125
slushyFalsecold8.099
Creating a DataFrame with a custom index column

Difference Between loc and iloc

The difference between the loc and iloc functions is that the loc function selects rows using row labels (e.g. tea) whereas the iloc function selects rows using their integer positions (staring from 0 and going up by one for each row).

Selecting a Single Row of Data

Selecting a Single Row by the Row Label (.loc)

For the loc function, specifying the row label as the input will index only the row with that label.

df.loc['tea']
carbonated?    False
temperature      hot
sugar(tsp)       2.0
calories          43
Name: tea, dtype: object
Indexing Using a Row Label

Selecting a Single Row by the Integer Index (.iloc)

On the other hand, for the iloc function, specifying a single integer index as the input will index the row at that position. For example, starting with zero, index 2 refers to the 3rd element in the list (the smoothie row):

# Retrieving the row at index 2
df.iloc[2]
carbonated?    False
temperature     cold
sugar(tsp)       6.0
calories          85
Name: smoothie, dtype: object
Indexing Using a Single Integer

Selecting a Single Row as a DataFrame

Note that the python code above returns the row as a Series. If we want the row returned as a DataFrame, we use a list with only one element:

# loc function
df.loc[ ['tea'] ]
carbonated?temperaturesugar(tsp)calories
drink
teaFalsehot2.043
Indexing Using a Single Label
# iloc function 
df.iloc[ [1] ]
carbonated?temperaturesugar(tsp)calories
drink
coffeeFalsehot3.031
Indexing Using a Single Integer

Selecting Multiple Rows of Data

Selecting Multiple Rows using a List

For both the loc and iloc functions, we can use a list as our input to retrieve multiple rows. The labels of the output are ordered by when the label appears in the input list.

# loc function
# note that multiple labels along the vertical axis is specified
df.loc[['smoothie', 'tea', 'soda']]
carbonated?temperaturesugar(tsp)calories
drink
smoothieFalsecold6.085
teaFalsehot2.043
sodaTruecold10.5150
Indexing Multiple Labels Using a List
# iloc function 
# note that the range is between 0 and (len(df) - 1) inclusive
df.iloc[[2, 4, 5]]
carbonated?temperaturesugar(tsp)calories
drink
smoothieFalsecold6.085
teaFalsehot2.043
lemonadeFalsecold9.5125
Indexing Multiple Integer Positions Using a List

Selecting Multiple Rows using a Slice

Alternatively, we can use a slice object as our input to retrieve multiple rows.
However, it is important to note that for the loc function, the start and stop are included in the output whereas the stop is not included for the iloc function.

# loc function
# note that 'coffee' and 'tea' are included in the output
df.loc['coffee':'tea']
carbonated?temperaturesugar(tsp)calories
drink
coffeeFalsehot3.031
smoothieFalsecold6.085
waterFalsecold0.00
teaFalsehot2.043
Indexing Multiple Labels Using a Slice Object
# iloc function 
# note that the output includes the row at index 3 but not the row at index 4
df.iloc[0:4]
carbonated?temperaturesugar(tsp)calories
drink
sodaTruecold10.5150
coffeeFalsehot3.031
smoothieFalsecold6.085
waterFalsecold0.00
Indexing Multiple Integer Positions Using a Slice Object

Selecting for BOTH Rows and Columns

Selecting a Single Cell Value

For the loc function, to find a specific cell, we can use a list as the input value, specifying the vertical axis label (left) and the horizontal axis label (right).
# loc function 
# 'coffee' is the vertical axis labe and 'temperature' is the horizontal axis label
df.loc['coffee', 'temperature']

'hot'

Indexing Using a Single Label (Row and Column)
For the iloc function, to find a specific cell, we can specify the row index (left) and column index (right) separated by a comma.
# iloc function 
# note that the following code uses single brackets ([]). Double brackets ([[]]) will retrieve rows between the specified integers. 
df.iloc[0,2]

10.5

Indexing Using a Single Integer (Row and Column)

Selecting a Multiple Cell Values using a List

Similar to indexing only rows, we can retrieve multiple rows and columns. One way to do this is using two lists (one for the vertical axis labels and one for the horizontal axis labels) separated by a comma as the input:

# loc function 
df.loc[['water', 'lemonade'], ['sugar(tsp)', 'calories']]
sugar(tsp)calories
drink
water0.00
lemonade9.5125
Indexing Using Lists (Row and Column)
# iloc function
df.iloc[[1, 0], [2, 3]]

sugar(tsp)calories
drink
coffee3.031
soda10.5150
Indexing Using Lists (Row and Column)

Selecting a Muliple Cell Values using a Slice

Additionally, we can use slice objects:

# loc function 
df.loc[:'water', 'carbonated?':'sugar(tsp)']
carbonated?temperaturesugar(tsp)
drink
sodaTruecold10.5
coffeeFalsehot3.0
smoothieFalsecold6.0
waterFalsecold0.0
Indexing Using Slice Objects (Row and Column)
# iloc function 
df.iloc[3:7, 2:]
sugar(tsp)calories
drink
water0.00
tea2.043
lemonade9.5125
slushy8.099
Indexing Using Slice Objects (Row and Column)

Selecting a Muliple Cell Values using a Combination of Everything

We can use a combination of the two methods together:

# loc function 
df.loc[['coffee', 'smoothie', 'soda'], :'sugar(tsp)']
carbonated?temperaturesugar(tsp)
drink
coffeeFalsehot3.0
smoothieFalsecold6.0
sodaTruecold10.5
Indexing Using a List and a Slice Object (Row and Column)
# iloc function 
df.iloc[2:5, [0, 1, 3]]
carbonated?temperaturecalories
drink
smoothieFalsecold85
waterFalsecold0
teaFalsehot43
Indexing Using a List and a Slice Object (Row and Column)

Pandas Documentation

Click Here for the full pandas documentation for the loc function
Click Here for the full pandas documentation for the iloc function