Pandas: Select rows based on a List of Indices

avatar
Borislav Hadzhiev

Last updated: Apr 12, 2024
4 min

banner

# Table of Contents

  1. Pandas: Select rows based on a List of Indices
  2. Pandas: Select rows based on a List of Indices using DataFrame.index.isin()
  3. Pandas: Select rows based on a List of Indices using df.loc
  4. Pandas: Select rows based on a List of Indices using df.take
  5. Pandas: Select rows based on a List of Indices using df.query

# Pandas: Select rows based on a List of Indices

Use the DataFrame.iloc position-based indexer to select rows in a DataFrame based on a list of indices.

The iloc property will return a new DataFrame containing only the rows at the specified indices.

main.py
import pandas as pd df = pd.DataFrame({ 'first_name': ['Alice', 'Bobby', 'Carl', 'Dan'], 'salary': [175.1, 180.2, 190.3, 205.5], 'experience': [10, 15, 20, 25] }) list_of_indices = [0, 2, 3] new_df = df.iloc[list_of_indices] # first_name salary experience # 0 Alice 175.1 10 # 2 Carl 190.3 20 # 3 Dan 205.5 25 print(new_df)

select rows based on list of indices in pandas

The code for this article is available on GitHub

The code sample selects the rows at index 0, 2 and 3.

The DataFrame.iloc property is an integer, position-based indexer.

The property takes values from 0 to the length of the axis minus 1.

The DataFrame.iloc property is used with one of the following inputs:

  • An integer (e.g. 0).
  • A list (or array) of integers (e.g. [0, 2, 3]).
  • A slice object containing integers (e.g. [0:2]).
  • A boolean array (e.g. [True, False, True]).
  • A callable function that takes 1 argument and returns valid output for indexing (one of the above).
  • A tuple of row and column indexes.
Note that the .iloc property raises an IndexError if the supplied indexer is out of bounds (except for slice indexers).

You might also see the following syntax being used.

main.py
import pandas as pd df = pd.DataFrame({ 'first_name': ['Alice', 'Bobby', 'Carl', 'Dan'], 'salary': [175.1, 180.2, 190.3, 205.5], 'experience': [10, 15, 20, 25] }) list_of_indices = [0, 2, 3] new_df = df.iloc[list_of_indices, :] # first_name salary experience # 0 Alice 175.1 10 # 2 Carl 190.3 20 # 3 Dan 205.5 25 print(new_df)

select rows by index list different syntax

The code for this article is available on GitHub

The code sample uses the colon : syntax to select the entire column axis.

# Pandas: Select rows based on a List of Indices using DataFrame.index.isin()

You can also use the DataFrame.index.isin() method to select DataFrame rows using a list of indices.

main.py
import pandas as pd df = pd.DataFrame({ 'first_name': ['Alice', 'Bobby', 'Carl', 'Dan'], 'salary': [175.1, 180.2, 190.3, 205.5], 'experience': [10, 15, 20, 25] }) list_of_indices = [0, 2, 3] new_df = df[df.index.isin(list_of_indices)] # first_name salary experience # 0 Alice 175.1 10 # 2 Carl 190.3 20 # 3 Dan 205.5 25 print(new_df)

select dataframe rows using list of indices with index isin

The code for this article is available on GitHub

The index.isin() method returns a boolean array that contains True for each value whose index is present in the supplied indices array.

main.py
import pandas as pd df = pd.DataFrame({ 'first_name': ['Alice', 'Bobby', 'Carl', 'Dan'], 'salary': [175.1, 180.2, 190.3, 205.5], 'experience': [10, 15, 20, 25] }) list_of_indices = [0, 2, 3] # [ True False True True] print(df.index.isin(list_of_indices))

The last step is to use bracket notation [] to select the array elements for which index.isin() has returned True.

main.py
new_df = df[df.index.isin(list_of_indices)]

# Pandas: Select rows based on a List of Indices using df.loc

You can also use the DataFrame.loc property to select rows based on a list of indices.

main.py
import pandas as pd df = pd.DataFrame({ 'first_name': ['Alice', 'Bobby', 'Carl', 'Dan'], 'salary': [175.1, 180.2, 190.3, 205.5], 'experience': [10, 15, 20, 25] }) list_of_indices = [0, 2, 3] new_df = df.loc[df.index[list_of_indices]] # first_name salary experience # 0 Alice 175.1 10 # 2 Carl 190.3 20 # 3 Dan 205.5 25 print(new_df)

select rows based on list of indices with df loc

The code for this article is available on GitHub

The DataFrame.loc property is primarily used for label-based indexing, but can also be passed an Index.

main.py
import pandas as pd df = pd.DataFrame({ 'first_name': ['Alice', 'Bobby', 'Carl', 'Dan'], 'salary': [175.1, 180.2, 190.3, 205.5], 'experience': [10, 15, 20, 25] }) list_of_indices = [0, 2, 3] print(df.index[list_of_indices]) new_df = df.loc[df.index[list_of_indices]] # first_name salary experience # 0 Alice 175.1 10 # 2 Carl 190.3 20 # 3 Dan 205.5 25 print(new_df)

# Pandas: Select rows based on a List of Indices using df.take

You can also use the DataFrame.take() method to select rows in a DataFrame based on a list of indices.

main.py
import pandas as pd df = pd.DataFrame({ 'first_name': ['Alice', 'Bobby', 'Carl', 'Dan'], 'salary': [175.1, 180.2, 190.3, 205.5], 'experience': [10, 15, 20, 25] }) list_of_indices = [0, 2, 3] new_df = df.take(list_of_indices) # first_name salary experience # 0 Alice 175.1 10 # 2 Carl 190.3 20 # 3 Dan 205.5 25 print(new_df)

select rows based on list of indices using df take

The code for this article is available on GitHub

The DataFrame.take() method returns the elements in the given positional indices along an axis.

By default, the axis argument is set to 0, which means that we are selecting rows.

If the axis argument is set to 1, then we are selecting columns.

# Pandas: Select rows based on a List of Indices using df.query

You can also use the DataFrame.query() method to select rows based on a list of indices.

main.py
import pandas as pd df = pd.DataFrame({ 'first_name': ['Alice', 'Bobby', 'Carl', 'Dan'], 'salary': [175.1, 180.2, 190.3, 205.5], 'experience': [10, 15, 20, 25] }) list_of_indices = [0, 2, 3] new_df = df.query('index in @list_of_indices') print(new_df)

select rows based on list of indices using df query

The code for this article is available on GitHub

The DataFrame.query() method queries the columns of the DataFrame with a boolean expression.

The method returns a new DataFrame that results from the provided query expression.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.