Get the Size of a DataFrame - Problem

Given a DataFrame called players with the following schema:

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

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

Return the result as an array: [number of rows, number of columns]

Input & Output

Example 1 — Basic DataFrame
$ Input: players = [{"player_id": 1, "name": "Alice", "age": 25, "position": "Forward"}, {"player_id": 2, "name": "Bob", "age": 30, "position": "Guard"}, {"player_id": 3, "name": "Charlie", "age": 28, "position": "Center"}]
Output: [3, 4]
💡 Note: The DataFrame has 3 rows (players) and 4 columns (player_id, name, age, position), so we return [3, 4]
Example 2 — Single Row
$ Input: players = [{"player_id": 1, "name": "Alice", "age": 25, "position": "Forward"}]
Output: [1, 4]
💡 Note: Only one player with 4 attributes, so dimensions are [1, 4]
Example 3 — Empty DataFrame
$ Input: players = []
Output: [0, 0]
💡 Note: Empty DataFrame has no rows and no columns, returning [0, 0]

Constraints

  • DataFrame can have 0 or more rows
  • DataFrame can have 0 or more columns
  • All rows must have the same number of columns
  • Column names are strings
  • Cell values can be any data type

Visualization

Tap to expand
Get the Size of a DataFrame INPUT player_id name age position 1 Alice 25 Forward 2 Bob 30 Guard 3 Charlie 28 Center DataFrame: players 3 rows 4 columns Column Types: player_id: int name: object age: int position: object ALGORITHM STEPS 1 Access DataFrame Use the players variable 2 Use .shape Property Built-in pandas attribute result = players.shape # Returns (rows, cols) 3 Shape Returns Tuple (3, 4) - rows and columns 4 Convert to List list(players.shape) .shape --> (rows, columns) FINAL RESULT DataFrame Dimensions: ROWS 3 COLS 4 Output Array: [3, 4] OK - Correct! 3 rows = 3 players 4 cols = player_id, name, age, position Key Insight: The pandas DataFrame.shape property returns a tuple (rows, columns) representing the dimensions. This is an O(1) operation - shape is stored as metadata, not computed by counting elements. TutorialsPoint - Get the Size of a DataFrame | Built-in Shape Property Approach
Asked in
Meta 15 Netflix 12 Spotify 8
33.0K Views
High Frequency
~5 min Avg. Time
892 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