Get the Size of a DataFrame solution and explanation
5 more videos on this problem
2878. Get the Size of a DataFrame | LeetCode | Python | PandasYou Data And AI · 1:05 · 1K views
Get the size of DataFrame. LeetCode 2878CuteLeetCrafter · 0:53 · 190 views
LeetCode 2878 | Get the Size of a DataFrame | PythonSleepy Cracker · 0:54 · 171 views
2. Get the Size of a DataFrame LeetCodeJohn Sharon Kumar J · 4:02 · 122 views
Leetcode 2878 - Get the size of a DataFrame [Pandas]Ptolémé · 1:46 · 29 views
2878 Get the size of a DataFrame | LeetCodeBrain Bloom · 1:09 · 13 views
The problem
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.
import pandas as pd
def get_dataframe_size(df):
return list(df.shape)
# Example usage
players = pd.DataFrame({
"player_id": [846, 749, 155, 583, 388, 883, 355, 247, 761, 642],
"name": ["Mason", "Riley", "Bob", "Isabella", "Zachary", "Ava", "Violet", "Thomas", "Jack", "Charlie"],
"age": [21, 30, 28, 32, 24, 23, 18, 27, 33, 36],
"position": ["Forward", "Winger", "Striker", "Goalkeeper", "Midfielder", "Defender", "Striker", "Striker", "Midfielder", "Center-back"],
"team": ["RealMadrid", "Barcelona", "ManchesterUnited", "Liverpool", "BayernMunich", "Chelsea", "Juventus", "ParisSaint-Germain", "ManchesterCity", "Arsenal"]
})
print(get_dataframe_size(players)) # Output: [10, 5]The function get_dataframe_size utilizes the shape attribute of a pandas DataFrame, which returns a tuple with the number of rows and columns.
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.
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.
import pandas as pd
def get_dataframe_size(df):
num_rows = len(df)
num_columns = len(df.columns)
return [num_rows, num_columns]
# Example usage
players = pd.DataFrame({
"player_id": [846, 749, 155, 583, 388, 883, 355, 247, 761, 642],
"name": ["Mason", "Riley", "Bob", "Isabella", "Zachary", "Ava", "Violet", "Thomas", "Jack", "Charlie"],
"age": [21, 30, 28, 32, 24, 23, 18, 27, 33, 36],
"position": ["Forward", "Winger", "Striker", "Goalkeeper", "Midfielder", "Defender", "Striker", "Striker", "Midfielder", "Center-back"],
"team": ["RealMadrid", "Barcelona", "ManchesterUnited", "Liverpool", "BayernMunich", "Chelsea", "Juventus", "ParisSaint-Germain", "ManchesterCity", "Arsenal"]
})
print(get_dataframe_size(players)) # Output: [10, 5]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.
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.
Complexity comparison
| 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. |
Complexity analysis
| 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 |
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.
