This tutorial will discuss about different ways to select DataFrame rows by index position or index number in Pandas.
Table Of Contents
Preparing DataSet
Let’s create a DataFrame with some dummy data.
import pandas as pd data = {'Col_A': [21, 12, 13, 14, 15, 16, 17], 'Col_B': [21, 22, 23, 24, 25, 26, 27], 'Col_C': [31, 32, 33, 34, 35, 36, 37]} index=["D1", "D2", "D3", "D4", "D5", "D6", "D7"] # Create DataFrame from dictionary df = pd.DataFrame.from_dict(data) # Set list index as Index of DataFrame df .set_index(pd.Index(index), inplace=True) print (df)
Output
Col_A Col_B Col_C D1 21 21 31 D2 12 22 32 D3 13 23 33 D4 14 24 34 D5 15 25 35 D6 16 26 36 D7 17 27 37
We will now select rows from this DataFrame by Index numbers or positions.
Select a Row by index Position in Pandas DataFrame
In Pandas, the DataFrame provides an attribute iloc[]
, which accepts an Index Number as an argument and returns a row at that particular Index in the DataFrame.
To select a single row from a DataFrame by index number, we can just pass its index number in the iloc[]
attribute. An important point to note is that the indexing starts from 0. So, if you want to select the 3rd row from the DataFrame, then you need to pass the index number 2.
Frequently Asked:
- Pandas: Get last N rows of dataframe
- Select Rows by Index List in Pandas
- Edit Cell Values in CSV files using Pandas in Python
- How to sum rows by specific columns in Pandas DataFrame?
Like in the below example, we will select a row at index number 2 from the DataFrame. Basically we are going to fetch the 3rd row of the DataFrame.
# Select row at index 2 i.e. # the 3rd row of DataFrame subDf = df.iloc[2] print (subDf)
Output
Col_A 13 Col_B 23 Col_C 33 Name: D3, dtype: int64
Select Multiple Rows by index numbers in Pandas DataFrame
We can also select multiple rows from a DataFrame by index numbers. For that we need to pass a list of index numbers in the iloc[]
attribute of the DataFrame. In the below example, we will select rows at index number 2, 4 and 6 from the DataFrame. We will pass a list containing these index numbers in the iloc[]
attribute of DataFrame. It will return a DataFrame containing the specified rows only.
# Select rows at index number 2, 4 and 6 subDf = df.iloc[[2, 4, 6]] print (subDf)
Output
Col_A Col_B Col_C D3 13 23 33 D5 15 25 35 D7 17 27 37
Select multiple rows by index range in Pandas DataFrame
We can also select multiple rows from a DataFrame by index number range. It means, we can select rows from one index number till another index number. For that, we need to pass the start
and end
index number in the iloc[] attribute (separated by a colon). Like,
df.iloc[start : end]
In the below example we will select all rows from a DataFrame from index number 2 till index number 5.
# Select rows from index number 2 till 5 subDf = df.iloc[2:5] print (subDf)
Output
Col_A Col_B Col_C D3 13 23 33 D4 14 24 34 D5 15 25 35
Summary
We learned about different ways to select rows from a DataFrame by index numbers or positions. Thanks.