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.

1. Quick example of Getting Shape of DataFrame

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


# Below are the quick examples 

# 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

11. 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

With 5 of experience in technical writing, I have had the privilege to work with a diverse range of technologies like Python, Pandas, NumPy and R. During this time, I have consistently demonstrated my ability to grasp intricate technical details and transform them into comprehensible materials.

Leave a Reply

You are currently viewing Pandas Get DataFrame Shape