.shape
Published Dec 30, 2022
Contribute to Docs
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 pdd = {"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:
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python:Pandas on Codecademy
- Career path
Data Scientist: Machine Learning Specialist
Machine Learning Data Scientists solve problems at scale, make predictions, find patterns, and more! They use Python, SQL, and algorithms.Includes 27 CoursesWith Professional CertificationBeginner Friendly90 hours - Course
Learn Python 3
Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.With CertificateBeginner Friendly23 hours