Python dataframe row number

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 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) 
 # 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.

Читайте также:  Install java on this computer
  • Pandas Set Index to Column in DataFrame
  • Pandas – Convert Index to Column in DataFrame
  • Convert Pandas Datetime Index to String
  • How to get the column names as list from pandas DataFrame
  • How to selecting a row of Pandas Series/DataFrame by integer index?
  • 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?

References

You may also like reading:

Источник

pandas.DataFrame.count#

The values None , NaN , NaT , and optionally numpy.inf (depending on pandas.options.mode.use_inf_as_na ) are considered NA.

Parameters axis , default 0

If 0 or ‘index’ counts are generated for each column. If 1 or ‘columns’ counts are generated for each row.

numeric_only bool, default False

Include only float , int or boolean data.

Returns Series or DataFrame

For each column/row the number of non-NA/null entries. If level is specified returns a DataFrame .

Number of non-NA elements in a Series.

Count unique combinations of columns.

Number of DataFrame rows and columns (including NA elements).

Boolean same-sized DataFrame showing places of NA elements.

Constructing DataFrame from a dictionary:

>>> df = pd.DataFrame("Person": . ["John", "Myla", "Lewis", "John", "Myla"], . "Age": [24., np.nan, 21., 33, 26], . "Single": [False, True, True, True, False]>) >>> df Person Age Single 0 John 24.0 False 1 Myla NaN True 2 Lewis 21.0 True 3 John 33.0 True 4 Myla 26.0 False 

Notice the uncounted NA values:

>>> df.count() Person 5 Age 4 Single 5 dtype: int64 

Counts for each row:

>>> df.count(axis='columns') 0 3 1 2 2 3 3 3 4 3 dtype: int64 

Источник

Pandas: Get the Row Number from a Dataframe

Pandas Get Row Numbers Cover Image

In this tutorial, you’ll learn how to use Pandas to get the row number (or, really, the index number) of a particular row or rows in a dataframe. There may be many times when you want to be able to know the row number of a particular value, and thankfully Pandas makes this quite easy, using the .index() function.

Practically speaking, this returns the index positions of the rows, rather than a row number as you may be familiar with in Excel. Because an index doesn’t really represent a row number, it doesn’t really represent a row number. That being said, Pandas doesn’t provide a true row number, so the index is closest match to this.

By the end of this tutorial, you’ll have learned:

  • How to get the row number(s) for rows matching a condition,
  • How to get only a single row number, and
  • How to count the number of rows matching a particular condition

The Quick Answer: Use .index to Get a Pandas Row Number

Quick Answer - Pandas Get Row Numbers

Loading a Sample Pandas Dataframe

To follow along with this tutorial, I have provided a sample Pandas Dataframe. If you want to follow along with the tutorial line by line, feel free to copy the code below. The dataframe is deliberately small so that it is easier to follow along with. Let’s get started!

# Loading a Sample Pandas Dataframe import pandas as pd df = pd.DataFrame.from_dict( < 'Name': ['Joan', 'Devi', 'Melissa', 'Dave', 'Nik', 'Kate', 'Evan'], 'Age':[19, 43, 27, 32, 28, 29, 42], 'Gender': ['Female', 'Female', 'Female', 'Male', 'Male', 'Female', 'Male'], 'Education': ['High School', 'College', 'PhD', 'High School', 'College', 'College', 'College'], 'City': ['Atlanta', 'Toronto', 'New York City', 'Madrid', 'Montreal', 'Vancouver', 'Paris'] >) print(df) # Returns: # Name Age Gender Education City # 0 Joan 19 Female High School Atlanta # 1 Devi 43 Female College Toronto # 2 Melissa 27 Female PhD New York City # 3 Dave 32 Male High School Madrid # 4 Nik 28 Male College Montreal # 5 Kate 29 Female College Vancouver # 6 Evan 42 Male College Paris

We can see that when we print the dataframe that we have a dataframe with six rows and five columns. Our columns contain completely unique variables and others that are more categorical.

In the next section, you’ll learn how to get the row numbers that match a condition in a Pandas Dataframe.

Get Row Numbers that Match a Condition in a Pandas Dataframe

In this section, you’ll learn how to use Pandas to get the row number of a row or rows that match a condition in a dataframe.

We can use conditional Pandas filtering (which I cover off in detail in this tutorial) to filter our dataframe and then select the index, or indices, of those rows. Let’s see how we can get the row numbers for all rows containing Males in the Gender column.

# Get the Row numbers matching a condition in a Pandas dataframe row_numbers = df[df['Gender'] == 'Male'].index print(row_numbers) # Returns: # Int64Index([3, 4, 6], dtype='int64')

We can see here that this returns three items: the indices for the rows matching the condition.

Now, let’s see how we can return the row numbers for rows matching multiple conditions. With this, we can use conditional filtering, by passing into multiple conditions. Let’s select rows where the conditions match being both Female and from Toronto:

# Get the Row numbers matching multiple conditions in a Pandas dataframe row_numbers = df[(df['Gender'] == 'Female') & (df['City'] == 'Toronto')].index print(row_numbers) # Returns: # Int64Index([1], dtype='int64')

We can see here that we were able to return the row numbers of a Pandas Dataframe that matches two conditions.

In the next section, you’ll learn how to use Pandas to get the first row number that matches a condition.

Get the First Row Number that Matches a Condition in a Pandas Dataframe

There may be times when you want to get only the first row number that matches a particular condition. This could be, for example, if you know how that only a single row will match this condition.

We say above, that we returned a Int64Index object, which is an indexable object. Because of this, we can easily access the index of the row number. Let’s see how:

# Get the row number of the first row that matches a condition row_numbers = df[df['Name'] == 'Kate'].index[0] print(row_numbers) # Returns: 5

We can see here, that when we index the index object we return just a single row number. This allows us to access and use this index position in different operations. For example, we could then use the row number to modify content within that record or be able to extract it programmatically.

In the next section, you’ll learn how to count the number of rows that match a condition.

Count the Number of Rows Matching a Condition

You may also find yourself in a situation where you need to be able to identify how many rows match a certain condition. This could be a helpful first step, for example, in identifying uniqueness of a row, if you want to make sure only a single row matches a given condition.

When we used the .index method above, we noticed that it returned a list-like object containing our row numbers. Because of this, we can pass this object into the len() function to count how many items exist in the array.

Let’s see how we can repeat an example above and count how many rows match that condition using Pandas:

# Count number of rows matching a condition row_numbers = df[(df['Gender'] == 'Female') & (df['City'] == 'Toronto')].index print(len(row_numbers)) # Returns: 1

We can see that by passing in the index object into the len() function, that we can confirm that only a single item matches our condition. This allows us to check for duplicates based on what we might assume to be a unique key. Otherwise, it may allow us to confirm whether enough rows match a given condition.

Conclusion

In this tutorial, you learned how to use Pandas to get the row numbers of a Pandas Dataframe that match a given condition. You also learned to get the row numbers of a rows that match multiple conditions. Finally, you learned how to use Pandas count the number of rows that match a given condition.

To learn more about the Pandas .index method, check out the official documentation here.

Источник

Оцените статью