• Post author:
  • Post category:Pandas
  • Post last modified:March 27, 2024
  • Reading time:16 mins read
You are currently viewing Pandas Get DataFrame Shape

We can get the shape of Pandas DataFrame using the shape attribute. The shape is nothing but a number of rows and columns of the DataFrame. It returns a tuple where the first element is the number of rows and the second is the number of columns. When it comes to Pandas Series, it will return a tuple of a number of rows.

If you are looking for numerber or columns * number of rows then use size attribute. In this article, I will explain Pandas shape attribute and using this how we can get the shape of DataFrame with several examples.

Key Points –

  • DataFrame shape in Pandas refers to the dimensions of the data structure, typically represented as (rows, columns).
  • Retrieving the shape of a DataFrame in Pandas is a fundamental operation to understand its size and structure.
  • The shape attribute of a DataFrame returns a tuple representing the number of rows and columns, respectively.
  • The row count is equivalent to the length of the DataFrame, while the column count is the number of columns.
  • Knowing the shape of a DataFrame is essential for various data manipulation and analysis tasks in Pandas.

1. Quick Example of Getting Shape of DataFrame

Following are the quick example of getting the shape of Pandas DataFarme.


# Quick example of getting shape of dataframe

# Example 1: Get the shape of Pandas dataframe
print(" Shape of DataFrame:", df.shape)

# Example 2: Get shape of Pandas Series
# df['column'] returns a Series
print(df['class'].shape)

# Example 3: Get empty DataFrame shape
print("Get the shape of empty DataFrame:", df.shape)
print("Get number of rows:", df.shape[0])
print("Get number of columns:", df.shape[1])

# Example 4: Get the size of Pandas dataframe
print(" Size of DataFrame:", df.size)

# Example 5: Get the information of the dataframe
print(df.info())

# Example 6: Get the length of rows
print(len(df))

# Example 7: Get the number of columns in a dataframe
print(len(df.columns))

# Example 8: Get the dimensions of dataframe
print(df.ndim)

2. Syntax of Pandas Shape Attribute

Following is the syntax of the DataFrame shape attribute.


# Following is the syntax of shape attribute
DataFrame.shape

2.1 Return Value

The DataFrame.shape returns the number of rows and columns as a tuple.

3. Get the Shape of Dataframe in Pandas 

The shape attribute is used to get the shape of Pandas DataFrame Series, it returns number of rows and columns in the form of tuple. For Series, it returns a tuple where, the elements are number of rows. Let’s apply this attribute on DataFrame.

As a first step let’s read csv file into Pandas DataFrame named student details, where the columns are id, name, class, gender, and marks.


# Read csv file into DataFrame
import pandas as pd
df= pd.read_csv("D://apps\student details.csv")
print(df)
print(df.head())
print(df.tail())

Yields below output.

Pandas DataFrame shape
Pandas DataFrame
Pandas DataFrame shape
Pandas DataFrame

Let’s apply shape attribute to the above Pandas DataFarme.


# Get the shape of Pandas dataframe
print(" Shape of DataFrame:", df.shape)

# Output:
# Shape of DataFrame: (35, 5)

Yields below output.

Shape of DataFrame

4. Get the Shape of Specific Column of DataFrame

A column in DataFrame is represented as a Series, so getting the shape of the DataFrame is same as getting the shape of the Series. For Series it will return the tuple of number of rows. Here, I will apply this attribute on one of the column of given DataFrame.


# Get shape of Pandas Series
print(df['class'].shape)

# Output:
# (35,)

5. Get the Shape of Empty Pandas DataFrame

However, we can get the shape of empty DataFrame using Pandas.shape attribute. Let’s create empty DataFrame then, apply shape attribute, it will return the empty tuple(0, 0).


# Create empty DateFrame
df = pd.DataFrame()
print(df)
# Get empty DataFrame shape
print("Get the shape of empty DataFrame:", df.shape)
print("Get number of rows:", df.shape[0])
print("Get number of columns:", df.shape[1])

# Output:
# Empty DataFrame
# Columns: []
# Index: []
# Get the shape of empty DataFrame: (0, 0)
# Get number of rows: 0
# Get number of columns: 0

6. Get Size of Pandas Dataframe

The size attribute is used to get the total number of elements in the DataFrame or Series. When we apply this attribute on DataFrames, it will return the the product of the number of rows and the number of columns. Whereas, Series it will return number of rows

Let’s apply size attribute on above DataFrame and get the size of Pandas DataFarme.


# Get the size of Pandas dataframe
print(" Size of DataFrame:", df.size)

# Output:
# Size of DataFrame: 175

7. Use the info Method

Pandas info() function is used to get the information of given DataFrame. This function can be returned number of columns, column labels, column data types, memory usage, range index, and the number of cells in each column (non-null values).


# Get the information of the dataframe
print(df.info())

# Output:
# RangeIndex: 35 entries, 0 to 34
# Data columns (total 5 columns):
#     Column  Non-Null Count  Dtype 
# ---  ------  --------------  ----- 
#  0   id      35 non-null     int64 
#  1   name    35 non-null     object
#  2   class   35 non-null     object
#  3   mark    35 non-null     int64 
#  4   gender  35 non-null     object
# dtypes: int64(2), object(3)
# memory usage: 1.5+ KB
# None

8. Get Number of Rows

Using len() function we can get the number of rows and columns of the Dataframe. Let’s apply and get the number of rows of a given DataFrame.


# Get the length of rows
print(len(df))

# Output:
# 35

9. Get Number of Columns in Pandas

Using len(df.columns) syntax we can get the number of columns in a DataFrame. here, df.columns returns the columns as list.


# Get the number of columns in a dataframe
print(len(df.columns))

# Output:
# 5

10. Get Number of Dimensions

Use ndim attribute to get the number of dimensions of a DataFrame. Let’ use this attribute and get the number of dimensions of DataFrame.


# Get the dimensions of dataframe
print(df.ndim)

# Output:
# 2

Frequently Asked Questions on Pandas Get DataFrame Shape

What does the shape attribute in Pandas DataFrame represent?

The shape attribute in Pandas DataFrame represents the dimensions of the DataFrame, typically denoted as (rows, columns). It indicates the number of rows and columns present in the DataFrame.

How can I retrieve the shape of a DataFrame in Pandas?

You can retrieve the shape of a DataFrame in Pandas by accessing its shape attribute, which returns a tuple representing the number of rows and columns.

Why is it important to know the shape of a DataFrame?

Knowing the shape of a DataFrame is crucial for understanding its size and structure, which is essential for various data manipulation and analysis tasks.

What does the row count in DataFrame shape signify?

The row count in DataFrame shape signifies the number of observations or records present in the DataFrame. Each row typically represents a single observation or data point in the dataset. Therefore, the row count indicates the total number of data points available for analysis within the DataFrame.

What information does the column count provide in DataFrame shape?

The column count in DataFrame shape indicates the number of variables or features present in the DataFrame. Each column represents a different variable or attribute of the data. Therefore, the column count provides information about the dimensions or characteristics being measured or observed in the dataset.

Conclusion

In this article, I have explained shape attribute and using this how we can get the shape of Pandas DataFrame, Series, empty DataFrame. And also explained using various attributes how we can get the size, dimensions, and total information of a given DataFrame with examples.

Happy learning!!

References

Vijetha

Vijetha is an experienced technical writer with a strong command of various programming languages. She has had the opportunity to work extensively with a diverse range of technologies, including Python, Pandas, NumPy, and R. Throughout her career, Vijetha has consistently exhibited a remarkable ability to comprehend intricate technical details and adeptly translate them into accessible and understandable materials. Follow me at Linkedin.