You can select rows from a list of Index in pandas DataFrame either using DataFrame.iloc[]
, DataFrame.loc[df.index[]]
. iloc[] takes row indexes as a list. loc[] takes row labels as a list, hence use df.index[] to get the column names for the indexes. In this article, I will explain how to use a list of indexes to select rows from pandas DataFrame with examples.
1. Quick Examples of Select Pandas Rows Based on List Index
If you are in a hurry, below are some quick examples of how to Select Pandas Rows Based on List Index in pandas DataFrame.
# Below are quick examples.
# How to select Pandas Rows Based on list using df.iloc[ind_list]
ind_list = [1, 3]
df.iloc[ind_list]
# How to select Pandas Rows Based on list using df.loc[df.index[index_list]]
index_list = [0,2]
df.loc[df.index[index_list]]
# Get Pandas rows on list index using index.isin().
df2=df[df.index.isin([1,3])]
print(df2)
# Get pandas rows on list index by df.time().
df2=df.take([1,2])
print(df2)
# Get Pandas rows on list index by df.query()
index_list = [1,2]
df2=df.query('index in @index_list')
print(df2)
Now, let’s create Pandas DataFrame with a few rows and columns, execute these examples and validate results. Our DataFrame contains column names Courses
, Fee
, Duration
, and Discount
.
import pandas as pd
technologies = {
'Courses':["Spark","PySpark","Python","pandas"],
'Fee' :[20000,25000,22000,30000],
'Duration':['30days','40days','35days','50days'],
'Discount':[1000,2300,1200,2000]
}
df = pd.DataFrame(technologies)
print(df)
Yields below output.
# Output:
Courses Fee Duration Discount
r1 Spark 20000 30days 1000
r2 PySpark 25000 40days 2300
r3 Python 22000 35days 1200
r4 pandas 30000 50days 2000
2. Using DataFrame.iloc[] to Select Rows From List Index
DataFrame.iloc[ind_list]
 method is used to filter/select rows from a list of index values. Pass the indexes you wanted to select as a list to this method. Let’s see with an example.
# Select Rows from List Index using df.iloc[iloc_lst]
ind_list = [1, 3]
df.iloc[ind_list]
Yields below output. This select row 2 and 4 as the index starts from zero.
# Output:
Courses Fee Duration Discount
r2 PySpark 25000 40days 2300
r4 Pandas 30000 50days 2000
3. Using df.loc[df.index[]] to Select Rows From List Index
Alternatively, you can select rows from the list index by using df.loc[df.index[]]
method. loc[] method is used to select the rows by labels. so in order to select by index, use df.index[]. This property returns row labels for a given index.
# Select rows from list using df.loc[df.index[index_list]]
index_list = [0,2]
df.loc[df.index[index_list]]
Yields below output.
# Output:
Courses Fee Duration Discount
r1 Spark 20000 30days 1000
r3 Python 22000 35days 1200
4. Get Pandas Rows on List Index Using isin()
You can select rows from a list index using index.isin()
Method which is used to check each element in the DataFrame is contained in values or not. This is the fasted approach. Note that this option doesn’t work if you have labels for index.
# Get Pandas rows on list index using index.isin().
df2=df[df.index.isin([1,3])]
print(df2)
Yields below output.
# Output:
Courses Fee Duration Discount
1 PySpark 25000 40days 2300
3 pandas 30000 50days 2000
5. Get Pandas Rows on List Index by DataFrame.take()
df.take()
function is also used to get the elements in the given positional indices along an axis. we are not indexing according to actual values in the index attribute of the object. We are indexing according to the actual position of the element in the object.
# Get pandas rows on list index by df.time().
df2=df.take([1,2])
print(df2)
Yields below output.
# Output:
Courses Fee Duration Discount
1 PySpark 25000 40days 2300
2 Python 22000 35days 1200
6. Get Pandas Rows on List Index by DataFrame.query()
Finally by using df.query()
function to query the columns of a DataFrame with a boolean expression to get rows by list of index. For example-
# Get Pandas rows on list index by df.query().
index_list = [1,2]
df2=df.query('index in @index_list')
Yields below output.
# Output:
Courses Fee Duration Discount
1 PySpark 25000 40days 2300
2 Python 22000 35days 1200
7. Complete Examples of Select Rows Based on List Index
import pandas as pd
technologies = {
'Courses':["Spark","PySpark","Python","pandas"],
'Fee' :[20000,25000,22000,30000],
'Duration':['30days','40days','35days','50days'],
'Discount':[1000,2300,1200,2000]
}
df = pd.DataFrame(technologies)
print(df)
# Select Rows from List Index using df.iloc[iloc_lst]
ind_list = [1, 3]
df.iloc[ind_list]
# Select rows from list using df.loc[df.index[index_list]]
index_list = [0,2]
df.loc[df.index[index_list]]
# Get Pandas rows on list index using index.isin().
df2=df[df.index.isin([1,3])]
print(df2)
# Get pandas rows on list index by df.time().
df2=df.take([1,2])
print(df2)
# Get Pandas rows on list index by df.query().
index_list = [1,2]
df2=df.query('index in @index_list')
Conclusion
In this article, you have learned how to use Select Pandas Rows Based on List Index using DataFrame.iloc[ind_list]
, DataFrame.loc[df.index[index_list]]
functions. Also, you have learned how to use Select Pandas Rows Based on List Index using DataFrame.isin()
and DataFrame.query()
with all above examples.
Related Articles
- Pandas Rename Column with Examples
- How to Merge Series into Pandas DataFrame
- Create DataFrame From Multiple Series in Pandas
- Pandas Operator Chaining to Filter DataFrame Rows
- Drop Infinite Values From Pandas DataFrame
- How to Drop Duplicate Columns in pandas DataFrame
- Pandas Get List of All Duplicate Rows
- How to Append Pandas Series?
- Pandas loc[] attribute multiple conditions
- Pandas Series loc[] attribute
- How to Slice Columns in Pandas DataFrame