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.
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.
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.
Python
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.
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.
Python
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.
| Approach | Complexity |
|---|---|
| Using Built-in DataFrame Attributes | Time Complexity: O(1) because accessing the shape attribute is a constant-time operation. |
| Using DataFrame Properties | Time Complexity: O(1) for both row and column size retrieval since the operations are constant time. |
| Approach | Time | Space | When to Use |
|---|---|---|---|
Using DataFrame shape Attribute | O(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 |
2878. Get the Size of a DataFrame | LeetCode | Python | Pandas • You Data And AI • 1,174 views views
Watch 4 more video solutions →Practice Get the Size of a DataFrame with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor