3.1 C
London
Friday, December 20, 2024
HomePandas in PythonGeneral Functions in PythonPandas loc vs. iloc: What’s the Difference?

Pandas loc vs. iloc: What’s the Difference?

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

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

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories