• Post author:
  • Post category:Pandas
  • Post last modified:March 27, 2024
  • Reading time:15 mins read
You are currently viewing Pandas Get Row Number of DataFrame

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 a 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 CoursesFeeDurationDiscount.


# 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

2. 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.

pandas get row number

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')

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 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'>

4. 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'>

5. 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

6. 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

How do I get the row number of a specific value in a DataFrame column?

You can use the index attribute along with the == operator to find the row number where a specific value occurs in a column. For example, row_number = df[df['ColumnName'] == 'specified_value'].index[0]

How can I get the row numbers of multiple values in a DataFrame 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. For example, specified_values = ['value1', 'value2', 'value3']
row_numbers = df[df['ColumnName'].isin(specified_values)].index.tolist()

What if I want to get the row number based on multiple conditions?

You can use the loc method with logical conditions to filter the DataFrame and then get the row numbers.

How can I get the row number based on the maximum or minimum value in a specific column?

You can use the idxmax() or idxmin() functions to get the index of the maximum or minimum value in a column, respectively. For example, max_row_number = df['ColumnName'].idxmax()
min_row_number = df['ColumnName'].idxmin()

How can I get the row numbers of NaN values in a DataFrame?

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()

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.

References

Naveen Nelamali

Naveen Nelamali (NNK) is a Data Engineer with 20+ years of experience in transforming data into actionable insights. Over the years, He has honed his expertise in designing, implementing, and maintaining data pipelines with frameworks like Apache Spark, PySpark, Pandas, R, Hive and Machine Learning. Naveen journey in the field of data engineering has been a continuous learning, innovation, and a strong commitment to data integrity. In this blog, he shares his experiences with the data as he come across. Follow Naveen @ LinkedIn and Medium