Get the Size of a DataFrame - Problem
Given a DataFrame called players with the following schema:
| 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 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
Understanding the Visualization
1
Input DataFrame
DataFrame with player data in rows and columns
2
Get Dimensions
Extract number of rows and columns
3
Output Array
Return [rows, columns] as result
Key Takeaway
🎯 Key Insight: Use DataFrame.shape property to get dimensions instantly in O(1) time
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code