Thursday 21 September 2023

How to Get the Number of Rows in a Pandas DataFrame

You can get the number of rows in a Dataframe using either ‘shape’ attribute or the ‘len()’ function.

 

Example 1: To get the total number of rows using the shape attribute

total_rows = df.shape[0]

 

Example 2: To fet the total number of rows using the len function

total_rows = len(df)

 

Find the below working application.

 

number_of_rows.py

import pandas as pd

# Create a sample DataFrame
data = {'Name': ['Krishna', 'Ram', 'Joel'],
        'Age': [34, 25, 29],
        'City': ['Bangalore', 'Chennai', 'Hyderabad']}

df = pd.DataFrame(data)

# Get the total number of rows using the shape attribute
total_rows = df.shape[0]
print("total_rows : ", total_rows)

# Get the total number of rows using the len function
total_rows = len(df)
print("total_rows : ", total_rows)

Output

total_rows :  3
total_rows :  3




 

 

Previous                                                 Next                                                 Home

No comments:

Post a Comment