How to Count Rows and Columns of a Pandas Dataframe in Python

There are different options to know the number of rows and columns of a Pandas DataFrame in Python. In this article, I’ll show you various approaches to know how to count the number of rows and columns of a Pandas DataFrame in Python. I have an another article How to Create a Pandas DataFrame in Python, where you will learn how to create a DataFrame. For this tutorial, create a sample DataFrame as below:

       id      name  math_score
0  202301    Minhaj          92
1  202302   Ridhwan          86
2  202303   Tanveer          76
3  202304  Sharodia          89
4  202305      Alve          99
5  202306   Intisar          99

Option-1: To see number of rows and number of column using df.info()

print(df.info())

Output:

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 6 entries, 0 to 5
Data columns (total 3 columns):
 #   Column      Non-Null Count  Dtype 
---  ------      --------------  ----- 
 0   id          6 non-null      object
 1   name        6 non-null      object
 2   math_score  6 non-null      int64 
dtypes: int64(1), object(2)
memory usage: 272.0+ bytes
None

Option-2: To see number of rows and number of column using len function

print(df)
print("Number of Rows: ",len(df))
print("Number of Columns: ",len(df.columns))

Output:
       id      name  math_score
0  202301    Minhaj          92
1  202302   Ridhwan          86
2  202303   Tanveer          76
3  202304  Sharodia          89
4  202305      Alve          99
5  202306   Intisar          99
Number of Rows:  6
Number of Columns:  3
Option-3: To see number of rows and number of column using len(df.axes[]) function
# computing number of rows
rows = len(df.axes[0])
# computing number of columns
cols = len(df.axes[1])
print(df)
print("Number of Rows: ", rows)
print("Number of Columns: ", cols)
Output:
       id      name  math_score
0  202301    Minhaj          92
1  202302   Ridhwan          86
2  202303   Tanveer          76
3  202304  Sharodia          89
4  202305      Alve          99
5  202306   Intisar          99
Number of Rows:  6
Number of Columns:  3

Option-4: To see number of rows and number of column using shape
print(df.head())

# obtaining the shape
print(“shape of dataframe”, df.shape)

# obtaining the number of rows
print(“number of rows : “, df.shape[0])

# obtaining the number of columns
print(“number of columns : “, df.shape[1])

Output:

       id      name  math_score
0  202301    Minhaj          92
1  202302   Ridhwan          86
2  202303   Tanveer          76
3  202304  Sharodia          89
4  202305      Alve          99
shape of dataframe (6, 3)
number of rows :  6
number of columns :  3

Option-5: To see multiplication of number of rows and number of column using size.

The size returns multiple rows and columns. i.e Here, the number of rows is 6, and the number of columns is 3 so the multiple rows and columns will be 6*3=18.

print(df.head())
print(df.size)

Output:

       id      name  math_score
0  202301    Minhaj          92
1  202302   Ridhwan          86
2  202303   Tanveer          76
3  202304  Sharodia          89
4  202305      Alve          99
18

Option-5: To see number of rows and number of column using count() and index

print(df.head())
print("number of rows : ",df[df.columns[0]].count())
print("number of columns : ",len(df.index))

Output:
       id      name  math_score
0  202301    Minhaj          92
1  202302   Ridhwan          86
2  202303   Tanveer          76
3  202304  Sharodia          89
4  202305      Alve          99
number of rows :  6
number of columns :  6

In this tutorial, I tried to brief how to count the number of rows and columns of a Pandas DataFrame in Python. Hope you have enjoyed the tutorial. If you want to get updated, like my facebook page https://www.facebook.com/LearningBigDataAnalytics and stay connected.

Add a Comment