If your are looking for the answer “How to row count in Pandas DataFrame”. So you are at the right place. This article covers the answer to your question. In this article we are going to cover different ways or methods to answer the question “row count in pandas dataframe”. Let’s see how to row count in Pandas.
It’s normal practice to count the rows and columns, especially when we want to share some insights with other people and teams. This suggests that we might want to give additional significance so that readers can relate them to particular situations more readily.
Lets discuses best methods to count rows in pandas dataframe:
Row count in Pandas DataFrame
Method 1: By using len() method of DataFrame
The number of rows of pandas.DataFrame can be obtained with the Python built-in function len().
# Import pandas package
import pandas as pd
# Define a dictionary containing students information
data = {'Name': ['Rahul', 'Pooja',
'Ketan', 'Mili'],
'RollNo': [101, 102, 103, 104],
'Subject': ['Hindi', 'English',
'Math', 'Science'],
'Marks': [79, 85, 89, 95]}
# Convert the dictionary into DataFrame
df = pd.DataFrame(data, columns=['Name', 'RollNo',
'Subject', 'Marks'])
print("\n Given Dataframe :\n", df)
print("Number of rows : ", len(df))
print("Number of columns : ", len(df.columns))
Output:
Given Dataframe :
Name RollNo Subject Percentage
0 Rahul 101 Hindi 79
1 Pooja 102 English 85
2 Ketan 103 Math 89
3 Mili 104 Science 95
Number of rows : 4
Number of columns : 4
Method 2: By using len(df.axes[]) method of DataFrame
To obtain the number of rows, and columns we can also use len(df.axes[]) function.
# Import pandas package
import pandas as pd
# Define a dictionary containing students information
data = {'Name': ['Rahul', 'Pooja',
'Ketan', 'Mili'],
'RollNo': [101, 102, 103, 104],
'Subject': ['Hindi', 'English',
'Math', 'Science'],
'Marks': [79, 85, 89, 95]}
# Convert the dictionary into DataFrame
df = pd.DataFrame(data, columns=['Name', 'RollNo',
'Subject', 'Marks'])
print("\n Given Dataframe :\n", df)
# counting the number of rows and columns
rows = len(df.axes[0])
cols = len(df.axes[1])
# printing the number of rows and columns
print("\n Number of Rows: ", rows)
print("\n Number of Columns: ", cols)
Output:
Given Dataframe :
Name RollNo Subject Percentage
0 Rahul 101 Hindi 79
1 Pooja 102 English 85
2 Ketan 103 Math 89
3 Mili 104 Science 95
Number of rows : 4
Number of columns : 4
Method 3: By using shape method of DataFrame
You can use pandas.DataFrame.shape that returns a tuple representing the dimensionality of the DataFrame. The first element of the tuple corresponds to the number of rows while the second element represents the number of columns. Let’s check how to use this function:
# Import pandas package
import pandas as pd
# Define a dictionary containing students information
data = {'Name': ['Rahul', 'Pooja',
'Ketan', 'Mili'],
'RollNo': [101, 102, 103, 104],
'Subject': ['Hindi', 'English',
'Math', 'Science'],
'Marks': [79, 85, 89, 95]}
# Convert the dictionary into DataFrame
df = pd.DataFrame(data, columns=['Name', 'RollNo',
'Subject', 'Marks'])
print("\n Given Dataframe :\n", df)
# acquring the shape
print("shape of dataframe is :", df.shape)
# counting the number of rows
print("number of rows : ", df.shape[0])
# counting the number of columns
print("number of columns : ", df.shape[1])
Output:
Given Dataframe :
Name RollNo Subject Percentage
0 Rahul 101 Hindi 79
1 Pooja 102 English 85
2 Ketan 103 Math 89
3 Mili 104 Science 95
shape of dataframe is : (4, 4)
number of rows : 4
number of columns : 4
Method 4: By using count() method of DataFrame
Another method that is used to count the number of rows and columns is pandas.DataFrame.count() method. Using count() and index we can get the number of rows present in the Dataframe. Let’s check how to use this function:
# Import pandas package
import pandas as pd
# Define a dictionary containing students information
data = {'Name': ['Rahul', 'Pooja',
'Ketan', 'Mili'],
'RollNo': [101, 102, 103, 104],
'Subject': ['Hindi', 'English',
'Math', 'Science'],
'Marks': [79, 85, 89, 95]}
# Convert the dictionary into DataFrame
df = pd.DataFrame(data, columns=['Name', 'RollNo',
'Subject', 'Marks'])
print("\n Given Dataframe :\n", df)
print("\n number of columns is :", df[df.columns[0]].count())
print("\n number of rows is : ", len(df.index))
Output:
Given Dataframe :
Name RollNo Subject Percentage
0 Rahul 101 Hindi 79
1 Pooja 102 English 85
2 Ketan 103 Math 89
3 Mili 104 Science 95
number of rows is : 4
number of columns is : 4
Final thoughts about : row count in pandas
We have covered a few methods or ways to count the rows and columns in pandas DataFrames in today’s article. len() function is the most effective way to count rows and columns in pandas. It is even quicker to provide simply the index (len(df.index)).
The count() function is the least effective, thus you should only use it if you need to exclude null entries from the counts.