Skip to main content

Get the Size of a DataFrame - Solution & Explanation

Easy5 min readAsked at: Google
Practice this problem

Problem Statement

DataFrame players:
+-------------+--------+
| Column Name | Type   |
+-------------+--------+
| player_id   | int    |
| name        | object |
| age         | int    |
| position    | object |
| ...         | ...    |
+-------------+--------+

Write a solution to calculate and display the number of rows and columns of players.

Return the result as an array:

[number of rows, number of columns]

The result format is in the following example.

 

Example 1:

Input:
+-----------+----------+-----+-------------+--------------------+
| player_id | name     | age | position    | team               |
+-----------+----------+-----+-------------+--------------------+
| 846       | Mason    | 21  | Forward     | RealMadrid         |
| 749       | Riley    | 30  | Winger      | Barcelona          |
| 155       | Bob      | 28  | Striker     | ManchesterUnited   |
| 583       | Isabella | 32  | Goalkeeper  | Liverpool          |
| 388       | Zachary  | 24  | Midfielder  | BayernMunich       |
| 883       | Ava      | 23  | Defender    | Chelsea            |
| 355       | Violet   | 18  | Striker     | Juventus           |
| 247       | Thomas   | 27  | Striker     | ParisSaint-Germain |
| 761       | Jack     | 33  | Midfielder  | ManchesterCity     |
| 642       | Charlie  | 36  | Center-back | Arsenal            |
+-----------+----------+-----+-------------+--------------------+
Output:
[10, 5]
Explanation:
This DataFrame contains 10 rows and 5 columns.

Approach Overview

Problem Overview: You receive a pandas DataFrame and need to return its size as a list containing the number of rows and columns. The task is straightforward because pandas already tracks this metadata internally. Your job is simply to access the correct attribute without performing any manual iteration.

Approach 1: Using Built-in DataFrame Attributes (O(1) time, O(1) space)

The most direct method uses the shape attribute of a pandas DataFrame. This attribute returns a tuple (rows, columns) representing the dataset dimensions. Since pandas stores this metadata internally, accessing df.shape does not require scanning the dataset or computing anything dynamically. You simply read the tuple and convert it to the required output format, typically a list like [rows, columns]. This approach is constant time O(1) because the values are already stored by pandas, and it uses constant space O(1) since no additional data structures are created. This is the cleanest and most idiomatic solution when working with Python and pandas.

Approach 2: Using DataFrame Properties (O(1) time, O(1) space)

Another option is accessing row and column counts individually using DataFrame properties such as len(df) for rows and len(df.columns) for columns. The expression len(df) returns the number of rows because a pandas DataFrame behaves like a collection of records. Meanwhile, df.columns stores the column index, and its length provides the number of columns. Both values are retrieved directly from stored metadata, so the operation still runs in constant time O(1) with O(1) extra space. This approach can feel more explicit because it clearly separates row and column counts, which some developers prefer when working with DataFrame structures in data processing pipelines.

Recommended for interviews: The df.shape approach is the one most interviewers expect. It demonstrates familiarity with pandas conventions and avoids unnecessary operations. The alternative property-based method still works and shows you understand how DataFrame structures expose row and column metadata. In practice, production code almost always uses shape because it is concise, readable, and the standard pattern across pandas codebases.

Approach 1: Using Built-in DataFrame Attributes

This approach utilizes the built-in attributes of a DataFrame in Python to extract the number of rows and columns. Specifically, DataFrame.shape is used to get a tuple containing the dimensions of the DataFrame.

The function get_dataframe_size utilizes the shape attribute of a pandas DataFrame, which returns a tuple with the number of rows and columns.

Code

Python

Complexity

Time Complexity: O(1) because accessing the shape attribute is a constant-time operation.
Space Complexity: O(1), as we only return a fixed-size list.

Try this approach in the editor →

Approach 2: Using DataFrame Properties

Another simple way to determine the number of rows and columns is by using DataFrame properties len() and DataFrame.columns to count each explicitly.

This approach calculates the number of rows by using the len() function on the DataFrame and the number of columns by counting the length of the columns property of the DataFrame.

Code

Python

Complexity

Time Complexity: O(1) for both row and column size retrieval since the operations are constant time.
Space Complexity: O(1), as we only store and return a fixed-size list.

Try this approach in the editor →

Approach 3: Default Approach

Code

Python

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Using Built-in DataFrame Attributes

Time Complexity: O(1) because accessing the shape attribute is a constant-time operation.
Space Complexity: O(1), as we only return a fixed-size list.

Using DataFrame Properties

Time Complexity: O(1) for both row and column size retrieval since the operations are constant time.
Space Complexity: O(1), as we only store and return a fixed-size list.

Default Approach—

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Using DataFrame shape AttributeO(1)O(1)Preferred pandas approach; concise and standard in production code
Using Row and Column Properties (len(df), len(df.columns))O(1)O(1)When you want explicit row and column calculations separately

Video Solution

2878. Get the Size of a DataFrame | LeetCode | Python | Pandas • You Data And AI • 1,178 views views

Watch 5 more video solutions →

Frequently Asked Questions

Get the Size of a DataFrame Python solution
In Python with pandas, return `[df.shape[0], df.shape[1]]` or convert `df.shape` directly to a list. Another valid solution is `[len(df), len(df.columns)]`. Both approaches run in O(1) time and O(1) space.
Is Get the Size of a DataFrame easy or hard?
Get the Size of a DataFrame is classified as an Easy problem. It mainly checks familiarity with pandas DataFrame attributes and properties rather than algorithmic complexity or data structure manipulation.
How to solve Get the Size of a DataFrame in O(1)?
Access the DataFrame metadata instead of computing dimensions manually. Use `df.shape` to get both values at once, or combine `len(df)` and `len(df.columns)` to retrieve rows and columns separately. Both operations run in constant time O(1).
What is the best approach for Get the Size of a DataFrame?
The best approach uses the pandas DataFrame attribute `df.shape`. It directly returns a tuple containing the number of rows and columns. Since pandas stores this metadata internally, retrieving it takes constant time O(1) and requires no iteration over the dataset.
Is Get the Size of a DataFrame asked at Google/Amazon/Meta?
This problem reflects fundamental pandas and data manipulation knowledge rather than a classic algorithm interview question. Similar tasks appear in data engineering, data science, and Python-focused interviews where candidates are expected to know common DataFrame operations.
What data structure is used in Get the Size of a DataFrame?
The problem uses a pandas DataFrame, a two-dimensional labeled data structure similar to a table or spreadsheet. Internally, pandas stores row and column metadata that allows constant-time access to the dataset dimensions.
What is the time complexity of Get the Size of a DataFrame?
The time complexity is O(1). Pandas maintains row and column counts as part of the DataFrame metadata, so accessing `df.shape`, `len(df)`, or `len(df.columns)` simply reads stored values instead of scanning the data.

Ready to solve this problem?

Practice Get the Size of a DataFrame with our built-in code editor and test cases.

Practice on FleetCode