When it comes to selecting rows and columns of a pandas DataFrame, loc and iloc are two commonly used functions.
Here is the subtle difference between the two functions:
- loc selects rows and columns with specific labels
- iloc selects rows and columns at specific integer positions
The following examples show how to use each function in practice.
Example 1: How to Use loc in Pandas
Suppose we have the following pandas DataFrame:
import pandas as pd #create DataFrame df = pd.DataFrame({'team': ['A', 'A', 'A', 'A', 'B', 'B', 'B', 'B'], 'points': [5, 7, 7, 9, 12, 9, 9, 4], 'assists': [11, 8, 10, 6, 6, 5, 9, 12]}, index=['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H']) #view DataFrame df team points assists A A 5 11 B A 7 8 C A 7 10 D A 9 6 E B 12 6 F B 9 5 G B 9 9 H B 4 12
We can use loc to select specific rows of the DataFrame based on their index labels:
#select rows with index labels 'E' and 'F'
df.loc[['E', 'F']]
team points assists
E B 12 6
F B 9 5
We can use loc to select specific rows and specific columns of the DataFrame based on their labels:
#select 'E' and 'F' rows and 'team' and 'assists' columns
df.loc[['E', 'F'], ['team', 'assists']]
team assists
E B 12
F B 9
We can use loc with the : argument to select ranges of rows and columns based on their labels:
#select 'E' and 'F' rows and 'team' and 'assists' columns
df.loc['E': , :'assists']
team points assists
E B 12 6
F B 9 5
G B 9 9
H B 4 12
Example 2: How to Use iloc in Pandas
Suppose we have the following pandas DataFrame:
import pandas as pd #create DataFrame df = pd.DataFrame({'team': ['A', 'A', 'A', 'A', 'B', 'B', 'B', 'B'], 'points': [5, 7, 7, 9, 12, 9, 9, 4], 'assists': [11, 8, 10, 6, 6, 5, 9, 12]}, index=['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H']) #view DataFrame df team points assists A A 5 11 B A 7 8 C A 7 10 D A 9 6 E B 12 6 F B 9 5 G B 9 9 H B 4 12
We can use iloc to select specific rows of the DataFrame based on their integer position:
#select rows in index positions 4 through 6 (not including 6)
df.iloc[4:6]
team points assists
E B 12 6
F B 9 5
We can use iloc to select specific rows and specific columns of the DataFrame based on their index positions:
#select rows in range 4 through 6 and columns in range 0 through 2
df.iloc[4:6, 0:2]
team assists
E B 12
F B 9
We can use loc with the : argument to select ranges of rows and columns based on their labels:
#select rows from 4 through end of rows and columns up to third column
df.iloc[4: , :3]
team points assists
E B 12 6
F B 9 5
G B 9 9
H B 4 12
Additional Resources
The following tutorials explain how to perform other common operations in pandas:
How to Select Rows by Multiple Conditions Using Pandas loc
How to Select Rows Based on Column Values in Pandas
How to Select Rows by Index in Pandas