Different ways to get the row count of a pandas DataFrame
The shape attribute
The shape attribute of the pandas DataFrame returns a tuple of two elements where the element at the zeroth index indicates the number of rows of the DataFrame.
DataFrame.shape
Note: Refer to What is pandas in Python to learn more about pandas.
Example
import pandas as pddf = pd.DataFrame({'Name':['Dom', 'Celeste', 'Abhi', 'Gaby', 'Rachel', 'Sam'],'Age': [20,25,30,18,25,20]})row_count, col_count = df.shapeprint("The number of rows in df is %s" % (row_count,))
Explanation
-
Line 3: We create a custom DataFrame.
-
Line 5: We use
df.shapeto return the number of rows and columns of a DataFrame.
Index length
The index length of the DataFrame can give the number of rows of the DataFrame.
len(df.index)
Example
import pandas as pddf = pd.DataFrame({'Name':['Dom', 'Celeste', 'Abhi', 'Gaby', 'Rachel', 'Sam'],'Age': [20,25,30,18,25,20]})row_count = len(df.index)print("The number of rows in df is %s" % (row_count,))
Explanation
-
Line 3: We create a custom DataFrame.
-
Line 5: We use
len(df.index)to return the number of rows of a DataFrame.
Using len() function
The len() function returns the number of objects. Hence, we can use len(dataframe) to get the number of rows of the DataFrame.
Example
import pandas as pddf = pd.DataFrame({'Name':['Dom', 'Celeste', 'Abhi', 'Gaby', 'Rachel', 'Sam'],'Age': [20,25,30,18,25,20]})row_count = len(df)print("The number of rows in df is %s" % (row_count,))
Explanation
-
Line 3: We create a custom DataFrame.
-
Line 5: we use
len(df)to returns the number of rows of a DataFrame.
The count() method
The count() method returns the non-NA count for the given column or row.
Note: Refer count.
df.count()
In the below dataframe, the count for Age is 5 as one of the values is None.
import pandas as pddf = pd.DataFrame({'Name':['Dom', 'Celeste', 'Abhi', 'Gaby', 'Rachel', 'Sam'],'Age': [20,25,30,None,25,20]})print(df.count())
Explanation
-
Line 3: We create a custom DataFrame.
-
Line 5: We use
df.count()to return the non-NA count for the given DataFrame.