0
0
Pandasdata~5 mins

shape for dimensions in Pandas

Choose your learning style9 modes available
Introduction

The shape tells you the size of your data in rows and columns. It helps you understand how big your table is.

When you want to know how many rows and columns your data has.
Before cleaning data, to check if any rows or columns are missing.
To confirm the size of data after filtering or selecting parts of it.
When you want to prepare data for machine learning and need to know input size.
Syntax
Pandas
dataframe.shape

shape returns a tuple: (number_of_rows, number_of_columns).

You can use it on pandas DataFrames or Series (Series shape is (number_of_rows,)).

Examples
This shows the shape of a DataFrame with 2 rows and 2 columns.
Pandas
import pandas as pd

df = pd.DataFrame({'A': [1, 2], 'B': [3, 4]})
print(df.shape)
This shows the shape of a Series, which only has rows (3,) because it has no columns.
Pandas
import pandas as pd

s = pd.Series([10, 20, 30])
print(s.shape)
Sample Program

This program creates a table with 3 rows and 4 columns, then prints its shape.

Pandas
import pandas as pd

# Create a DataFrame with 3 rows and 4 columns
data = {'Name': ['Alice', 'Bob', 'Charlie'],
        'Age': [25, 30, 35],
        'City': ['NY', 'LA', 'Chicago'],
        'Score': [85, 90, 95]}
df = pd.DataFrame(data)

# Print the shape of the DataFrame
print(df.shape)
OutputSuccess
Important Notes

The first number in shape is always the number of rows (data entries).

The second number is the number of columns (features or variables).

You can use shape to quickly check if your data loaded correctly.

Summary

shape tells you the size of your data as (rows, columns).

Use it to understand how much data you have before analysis.

Works on both DataFrames and Series in pandas.