You can get the row number of the Pandas DataFrame using the df.index
property. Using this property we can get the row number of a certain value based on a particular column. If you want to get the number of rows you can use the len(df.index)
function. In this article, I will explain how to get the row number from the DataFrame with several examples.
1. Quick Examples of How To Get Row Number of DataFrame
If you are in hurry, below are some quick examples of how to get row numbers from pandas DataFrame.
# Below are some quick examples.
# Example 1: Get the row number of value based on column
row_num = df[df['Duration'] == '35days'].index
# Example 2: Get the row number using multiple conditions
row_num = df[(df['Duration'] == '35days') & (df['Courses'] == 'Pandas')].index
# Example 3: Get row number as a NumPy array
row_num = df[df['Discount'] == 1200].index.to_numpy()
# Example 4: Get row number as a list
row_num = df[df['Fee'] == 24000]
print(row_num.index.tolist())
# Example 5: Get Maximum row number using idxmax()
row_num = df['Fee'].idxmax()
# Example 6: Get Minimum row number using idxmin()
row_num = df['Fee'].idxmin()
Let’s create a Pandas DataFrame with a Python dictionary of lists, pandas DataFrame columns names Courses
, Fee
, Duration
, Discount
.
# Create DataFrame
import pandas as pd
import numpy as np
technologies= {
'Courses':["Spark","PySpark","Hadoop","Python","Pandas"],
'Fee' :[22000,25000,23000,24000,26000],
'Duration':['30days','50days','35days', '40days','35days'],
'Discount':[1000,2300,1000,1200,2500]
}
df = pd.DataFrame(technologies)
print(df)
Yields below output.
# Output:
Courses Fee Duration Discount
0 Spark 22000 30days 1000
1 PySpark 25000 50days 2300
2 Hadoop 23000 35days 1000
3 Python 24000 40days 1200
4 Pandas 26000 35days 2500
2. Pandas Get Row Number
In order to get the row number from the Pandas DataFrame use df.index
property. For example, I want to get the row number that has ’35days’ value in the ‘Duration’ column. Let’s use the property to get the row number from DataFrame based on the condition.
# Get the row number of value based on column
row_num = df[df['Duration'] == '35days'].index
print(row_num)
# Output:
# Int64Index([2, 4], dtype='int64')
Since we have two rows with the same value, it returned the row number for two matched values.
We can also use multiple conditions to get the row number that matches the value. Let’s see how it can be returned the row number using multiple conditions.
# Get the row number using multiple condition
row_num = df[(df['Duration'] == '35days') & (df['Courses'] == 'Pandas')].index
print(row_num)
# Output:
# Int64Index([4], dtype='int64')
3. Get Pandas Row Number as NumPy Array
Using to_numpy()
function along with the property we can get the row number from DataFrame as NumPy Array. The below example get the row number as a NumPy array.
# Get row number as a NumPy array
row_num = df[df['Discount'] == 1200].index.to_numpy()
print(row_num)
print(type(row_num))
# Output:
# [3]
4. Get Pandas Row Number as a List
Using tolist() function along with the property we can get the row number of a certain value based on a specified column in a DataFrame. This syntax will return the row number as a list.
# Get row number as a list
row_num = df[df['Fee'] == 24000]
print(row_num.index.tolist())
print(type(row_num.index.tolist()))
# Output:
# [3]
5. Get Maximum Row number of Pandas use idxmax()
We can also get the maximum row number in a given DataFrame based on a specified column using the idxmax()
function. Let’s call the idxmax() function with the specified column of the given DataFrame, it will return the maximum row number.
# Get Maximum row number use idxmax()
row_num = df['Fee'].idxmax()
print(row_num)
# Output:
# 4
6. Get Minimum Row number using idxmin()
We can also get the minimum row number of a given DataFrame based on a specified column using the idxmin()
function. Let’s call the idxmin() function with the specified column of the given DataFrame, it will return the minimum row number of the specified column.
# Get Minimum row number use idxmin()
row_num = df['Fee'].idxmin()
print(row_num)
# Output:
# 0
7. Conclusion
In this article, I have explained how we can get the row number of a certain value based on a particular column from Pandas DataFrame. Also, I explained how to get the row number as a NumPy array and list using to_numpy()
and tolist()
functions and how to get the max and min row number of a DataFrame.