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)
method. In this article, I will explain the row number from the DataFrame with several examples.
Key Points –
- Access row numbers using the
.index
attribute of a DataFrame. .index
returns the row index labels or positions.- Retrieve row numbers based on specific conditions using boolean indexing or other selection methods.
- Utilize the
.iloc[]
method to extract rows by their integer position. - Access the row numbers directly using the
.index
attribute of the DataFrame. - Use
df.loc[condition].index
to find the index labels that match a condition. - Row numbers refer to the physical position, while index labels can be custom or non-numeric.
Quick Examples of Get Row Number of DataFrame
If you are in a hurry, below are some quick examples of how to get row numbers from Pandas DataFrame.
# Quick examples of get row number of dataframe
# 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()
To run some examples of getting the row number of pandas DataFrame, let’s create DataFrame with a Python dictionary of lists.
# 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("Create DataFrame:\n", df)
Yields below output.
Pandas Get Row Number
In order to get the row number from the Pandas DataFrame use the df.index
property. For example, I want to get the row number that has a ’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("Get row number of specified value:\n", row_num)
Yields below output.
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 return the row number using multiple conditions.
# Get the row number using multiple condition
row_num = df[(df['Duration'] == '35days') & (df['Courses'] == 'Pandas')].index
print("Get row number of specified value:\n", row_num)
# Output:
# Get row number of specified value:
# Int64Index([4], dtype='int64')
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 gets the row number as a NumPy array.
# Get row number as a NumPy array
row_num = df[df['Discount'] == 1200].index.to_numpy()
print("Get row number of specified value:\n", row_num)
print(type(row_num))
# Output:
# Get row number of specified value:
# [3]
# <class 'numpy.ndarray'>
Get Pandas Row Number as a List
Using the 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]
# <class 'list'>
Get Maximum Row number of Pandas using 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("Get row number of specified value:\n", row_num)
# Output:
# Get row number of specified value:
# 4
Get the 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("Get row number of specified value:\n", row_num)
# Output:
# Get row number of specified value:
# 0
Frequently Asked Questions on Pandas Get Row Number
You can use the index
attribute along with the ==
operator to find the row number where a specific value occurs in a column.
You can use the isin()
method to check if values are present in a column and then use index
attribute to get the row numbers.
You can use the loc
method with logical conditions to filter the DataFrame and then get the row numbers.
You can use the idxmax()
or idxmin()
functions to get the index of the maximum or minimum value in a column, respectively.
You can use the isna()
method to check for NaN values and then use index
to get the row numbers. For example, nan_row_numbers = df[df['ColumnName'].isna()].index.tolist()
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.
Related Articles
- Pandas Set Index to Column in DataFrame
- Convert Pandas Datetime Index to String
- Pandas Drop Rows Based on Column Value
- How to append DataFrames using for loop?
- How to get a first row in a Pandas DataFrame?
- How to get a last row in a Pandas DataFrame?
- Get unique rows in Pandas DataFrame
- Get First N row From Pandas DataFrame
- How to drop first row from the Pandas DataFrame?
- Pandas get the number of rows from DataFrame
- Pandas – Convert Index to Column in DataFrame
- How to get the column names as list from pandas DataFrame
- How to select a row of Pandas Series/DataFrame by integer index?