Monday 25 September 2023

Pandas: Get the Number of Rows and Columns in a DataFrame

Using the ‘shape’ attribute of the DataFrame, we can get the shape or number of rows and columns of a DataFrame.

 

Example

shape = df.shape

 

dataframe_shape.py

import pandas as pd

# Create a sample DataFrame
data = {'Name': ['Krishna', 'Ram', 'Joel', 'Gopi', 'Jitendra', "Raj"],
        'Age': [34, 25, 29, 41, 52, 23],
        'City': ['Bangalore', 'Chennai', 'Hyderabad', 'Hyderabad', 'Bangalore', 'Chennai']}

df = pd.DataFrame(data)
shape = df.shape

print("shape : ", shape)
print("rows : ", shape[0])
print("columns : ", shape[1])

 

Output

shape :  (6, 3)
rows :  6
columns :  3

 

 

Previous                                                 Next                                                 Home

No comments:

Post a Comment