Last updated: Apr 12, 2024
Reading time·4 min
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.
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)
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:
0
).[0, 2, 3]
).[0:2]
).[True, False, True]
)..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.
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)
The code sample uses the colon :
syntax to select the entire column axis.
DataFrame.index.isin()
You can also use the DataFrame.index.isin() method to select DataFrame rows using a list of indices.
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)
The index.isin()
method returns a boolean array that contains True
for each
value whose index is present in the supplied indices array.
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
.
new_df = df[df.index.isin(list_of_indices)]
df.loc
You can also use the DataFrame.loc property to select rows based on a list of indices.
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)
The DataFrame.loc
property is primarily used for label-based indexing, but can
also be passed an Index
.
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)
df.take
You can also use the
DataFrame.take()
method to select rows in a DataFrame
based on a list of indices.
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)
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.
df.query
You can also use the DataFrame.query() method to select rows based on a list of indices.
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)
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.
You can learn more about the related topics by checking out the following tutorials: