Get the Size of a DataFrame - Problem

You're given a pandas DataFrame called players that contains information about sports players. Your task is to determine the dimensions of this DataFrame - specifically, how many rows and columns it contains.

DataFrame Schema:

Column NameType
player_idint
nameobject
ageint
positionobject
......

Goal: Write a function that returns the DataFrame's dimensions as a list: [number_of_rows, number_of_columns]

This is a fundamental pandas operation that you'll use frequently in data analysis to understand the size and structure of your datasets.

Input & Output

example_1.py โ€” Small DataFrame
$ Input: players = pd.DataFrame({ 'player_id': [1, 2, 3], 'name': ['Alice', 'Bob', 'Carol'], 'age': [25, 27, 23], 'position': ['Forward', 'Midfielder', 'Defender'] })
โ€บ Output: [3, 4]
๐Ÿ’ก Note: The DataFrame has 3 rows (players) and 4 columns (player_id, name, age, position)
example_2.py โ€” Single Row DataFrame
$ Input: players = pd.DataFrame({ 'player_id': [1], 'name': ['Alice'], 'age': [25] })
โ€บ Output: [1, 3]
๐Ÿ’ก Note: The DataFrame has 1 row and 3 columns
example_3.py โ€” Empty DataFrame
$ Input: players = pd.DataFrame(columns=['player_id', 'name', 'age', 'position'])
โ€บ Output: [0, 4]
๐Ÿ’ก Note: The DataFrame has 0 rows but still has 4 defined columns

Visualization

Tap to expand
DataFrame Dimensions VisualizationDataFrame StructureIDNameAgePosition1Alice25Forward2Bob27Midfielder3Carol23Defender4Dave29Goalkeeper5Eve24Defender5 ROWS4 COLUMNS.shapeShape Property(5, 4)(rows, columns)โ†’ [5, 4]๐ŸŽฏ Key Insight: .shape gives instant O(1) access to DataFrame dimensions!
Understanding the Visualization
1
Identify DataFrame Structure
DataFrame is like a table with rows representing records and columns representing attributes
2
Access Shape Metadata
Pandas stores dimension information as metadata, accessible via .shape property
3
Return Dimensions
Convert the (rows, cols) tuple to [rows, cols] list format
Key Takeaway
๐ŸŽฏ Key Insight: The .shape property provides instant access to DataFrame dimensions without any computation, making it the optimal O(1) solution for this problem.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(1)

Shape is stored as metadata, accessed in constant time

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

Only creating a small list to return the result

n
2n
โœ“ Linear Space

Constraints

  • The DataFrame will be a valid pandas DataFrame object
  • Number of rows: 0 โ‰ค rows โ‰ค 106
  • Number of columns: 1 โ‰ค columns โ‰ค 103
  • The DataFrame structure is guaranteed to be consistent
Asked in
Netflix 45 Spotify 38 Airbnb 32 Uber 28
68.5K Views
High Frequency
~5 min Avg. Time
2.3K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen