.shape

The .shape property returns a tuple of information about the dimensions (rows and columns) of a DataFrame object.

Syntax

dataFrameValue.shape

The dataFrameValue must be a valid DataFrame object. If the dataFrameValue has a different length for one or more columns, a ValueError will be thrown.

Example

The following example initiates a DataFrame and uses .shape to return the number of rows and columns:

import pandas as pd
d = {"col1" : [1, 2 ,3], "col2" : [4, 5, 6]}
df = pd.DataFrame(data = d)
print(df.shape)

This will print the following:

(3, 2)

The result can be interpreted that DataFrame has 3 rows and 2 columns.

Codebyte Example

The following example throws a ValueError because the DataFrame has columns of differing length:

Code
Output
Loading...

Contributors

Interested in helping build Docs? Read the Contribution Guide or share your thoughts in this feedback form.

Learn Python:Pandas on Codecademy