Different methods to count rows in pandas DataFrame
In this tutorial we will discuss how to count rows in panads dataframe using the following methods:
- Using
axes()
function - Using
shape()
function - Using
index
- Using
info()
method - Using
len()
function - Using
count()
function - Using
value_counts()
function
Create pandas DataFrame with example data
DataFrame is a data structure used to store the data in two dimensional format. It is similar to table that stores the data in rows and columns. Rows represents the records/ tuples and columns refers to the attributes.
We can create the DataFrame by using pandas.DataFrame() method.
Syntax:
pandas.DataFrame(input_data,columns,index)
Parameters:
It will take mainly three parameters
- input_data is represents a list of data
columns
represent the columns names for the dataindex
represent the row numbers/values
We can also create a DataFrame using dictionary by skipping columns and indices.
Example: Python Program to create a dataframe for market data from a dictionary of food items by specifying the column names.
# import the module
import pandas
# consider the food data
food_input={'id':['foo-23','foo-13','foo-02','foo-31'],
'name':['ground-nut oil','almonds','flour','cereals'],
'cost':[567.00,562.56,67.00,76.09],
'quantity':[1,2,3,2]}
# pass this food to the dataframe by specifying rows
dataframe=pandas.DataFrame(food_input,index = ['item-1', 'item-2', 'item-3', 'item-4'])
# display the dataframe
print(dataframe)
Output:
id name cost quantity
item-1 foo-23 ground-nut oil 567.00 1
item-2 foo-13 almonds 562.56 2
item-3 foo-02 flour 67.00 3
item-4 foo-31 cereals 76.09 2
Method 1 : count rows in pandas DataFrame using axes() function
Here we are going to display the count of total number of rows available in pandas dataframe. So we are using axes()
function to get the count inside the len()
function. It will get the length of rows
we have to specify index - 0
inside this function to get row count.
Syntax:
len(dataframe.axes[0])
where,
- dataframe is the input dataframe
- [0] specifies the row
Example: In this program we are going to get the count the rows from the above dataframe
# import the module
import pandas
# consider the food data
food_input={'id':['foo-23','foo-13','foo-02','foo-31'],
'name':['ground-nut oil','almonds','flour','cereals'],
'cost':[567.00,562.56,67.00,76.09],
'quantity':[1,2,3,2]}
# pass this food to the dataframe by specifying rows
dataframe=pandas.DataFrame(food_input,index = ['item-1', 'item-2', 'item-3', 'item-4'])
# count the rows from the dataframe
print(len(dataframe.axes[0]))
Output:
4
Method 2 : count rows in pandas DataFrame using shape() function
Here we are going to display the count of total number of rows available in pandas dataframe. So we are using shape()
function to get the count. shape() will return number of rows and number of columns from the given dataframe. If we want to get the number of rows, we have to specify index - 0
inside this function to get row count.
Syntax:
dataframe.shape[0]
where,
- dataframe is the input dataframe
- [0] specifies the row
Example 1: In this program we are going to get the count the rows from the above dataframe
# import the module
import pandas
# consider the food data
food_input={'id':['foo-23','foo-13','foo-02','foo-31'],
'name':['ground-nut oil','almonds','flour','cereals'],
'cost':[567.00,562.56,67.00,76.09],
'quantity':[1,2,3,2]}
# pass this food to the dataframe by specifying rows
dataframe=pandas.DataFrame(food_input,index = ['item-1', 'item-2', 'item-3', 'item-4'])
# count the rows from the dataframe
print(dataframe.shape[0])
Output:
4
If we want to get the count of rows and columns , then we have to simply use shape.
Example 2: In this program we are going to get the count the rows and columns from the above dataframe
# import the module
import pandas
# consider the food data
food_input={'id':['foo-23','foo-13','foo-02','foo-31'],
'name':['ground-nut oil','almonds','flour','cereals'],
'cost':[567.00,562.56,67.00,76.09],
'quantity':[1,2,3,2]}
# pass this food to the dataframe by specifying rows
dataframe=pandas.DataFrame(food_input,index = ['item-1', 'item-2', 'item-3', 'item-4'])
# count the rows and columns from the dataframe
print(dataframe.shape)
Output:
(4, 4)
Method 3 : count rows in pandas DataFrame using index
Here we are going to display the count of total number of rows available in pandas dataframe. So we are using index
function to get the row inside with len()
function
Syntax:
len(dataframe.index)
where, dataframe is the input dataframe
Example: In this program we are going to get the count the rows from the above dataframe
# import the module
import pandas
# consider the food data
food_input={'id':['foo-23','foo-13','foo-02','foo-31'],
'name':['ground-nut oil','almonds','flour','cereals'],
'cost':[567.00,562.56,67.00,76.09],
'quantity':[1,2,3,2]}
# pass this food to the dataframe by specifying rows
dataframe=pandas.DataFrame(food_input,index = ['item-1', 'item-2', 'item-3', 'item-4'])
# count the rows from the dataframe
print(len(dataframe.index))
Output:
4
Method 4 : count rows in pandas DataFrame using info() method
info()
is used to get the total rows along with columns names and its data types. It will retunr the class type i.e the Pandas DataFrame , data types of the each column by displaying the column name with non null vales count. Atlast it will return the size of memory utilized.
Syntax:
dataframe.info()
where, dataframe is the input dataframe
Example: Python program to get the row count with info() method
# import the module
import pandas
# consider the food data
food_input={'id':['foo-23','foo-13','foo-02','foo-31'],
'name':['ground-nut oil','almonds','flour','cereals'],
'cost':[567.00,562.56,67.00,76.09],
'quantity':[1,2,3,2]}
# pass this food to the dataframe by specifying rows
dataframe=pandas.DataFrame(food_input,index = ['item-1', 'item-2', 'item-3', 'item-4'])
# count the rows from the dataframe
print(dataframe.info())
Output:
<class 'pandas.core.frame.DataFrame'>
Index: 4 entries, item-1 to item-4
Data columns (total 4 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 id 4 non-null object
1 name 4 non-null object
2 cost 4 non-null float64
3 quantity 4 non-null int64
dtypes: float64(1), int64(1), object(2)
memory usage: 160.0+ bytes
None
Method 5 : count rows in pandas DataFrame using len() function
In this scenario, we will be using len()
function to get the total number of rows from the pandas dataframe
Syntax:
len(dataframe)
where, dataframe is the input dataframe
Example: Python program to get the count of rows using len() function
# import the module
import pandas
# consider the food data
food_input={'id':['foo-23','foo-13','foo-02','foo-31'],
'name':['ground-nut oil','almonds','flour','cereals'],
'cost':[567.00,562.56,67.00,76.09],
'quantity':[1,2,3,2]}
# pass this food to the dataframe by specifying rows
dataframe=pandas.DataFrame(food_input,index = ['item-1', 'item-2', 'item-3', 'item-4'])
# count the rows from the dataframe
print(len(dataframe))
Output:
4
Method 6 : count rows in pandas DataFrame using count() function
Here we have to use count()
function inorder to get the count of total rows in the pandas dataframe. It will return the count in all columns and display column wise.
Syntax:
DataFrame.count(axis=0, level=None, numeric_only=False)
where,
- dataframe is the input dataframe.
- axis: If 0 or ‘index’ counts are generated for each column. If 1 or ‘columns’ counts are generated for each row.
- level: If the axis is a MultiIndex (hierarchical), count along a particular level, collapsing into a DataFrame. A str specifies the level name.
- numeric_only: Include only float, int or boolean data.
Example: Python program to use count() function to return the number of rows across all columns
# import the module
import pandas
# consider the food data
food_input={'id':['foo-23','foo-13','foo-02','foo-31'],
'name':['ground-nut oil','almonds','flour','cereals'],
'cost':[567.00,562.56,67.00,76.09],
'quantity':[1,2,3,2]}
# pass this food to the dataframe by specifying rows
dataframe=pandas.DataFrame(food_input,index = ['item-1', 'item-2', 'item-3', 'item-4'])
# count the rows from the dataframe
print(dataframe.count())
Output:
id 4
name 4
cost 4
quantity 4
dtype: int64
Method 7 : count rows in pandas DataFrame using value_counts() function
value_counts()
function is used count of row occurrence across the columns in the pandas dataframe. It will return the row and columns along with count of occurrence. This function will return the count of each row at the end of each row.
Syntax:
DataFrame.value_counts(subset=None, normalize=False, sort=True, ascending=False, dropna=True)
where,
- subset: Columns to use when counting unique combinations.
- normalize: Return proportions rather than frequencies.
- sort: Sort by frequencies.
- ascending: Sort in ascending order.
- dropna: Don’t include counts of rows that contain NA values.
Example: Python program to count rows using value_counts()
# import the module
import pandas
# consider the food data
food_input={'id':['foo-23','foo-13','foo-02','foo-31'],
'name':['ground-nut oil','almonds','flour','cereals'],
'cost':[567.00,562.56,67.00,76.09],
'quantity':[1,2,3,2]}
# pass this food to the dataframe by specifying rows
dataframe=pandas.DataFrame(food_input,index = ['item-1', 'item-2', 'item-3', 'item-4'])
# count the rows from the dataframe
print(dataframe.value_counts())
Output:
id name cost quantity
foo-31 cereals 76.09 2 1
foo-23 ground-nut oil 567.00 1 1
foo-13 almonds 562.56 2 1
foo-02 flour 67.00 3 1
dtype: int64
Summary
In this tutorial we discussed how to count rows from the dataframe using axes,shape,len,count,value_counts(),index
and info()
functions. We have used len()
function in functions like axes and index. Finally, we also observed that by using info()
and value_counts()
functions will return other information related to the dataframe other than row count.
Reference