Get the Size of a DataFrame
EasyUpdated: Aug 2, 2025
Practice on:
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.
Examples
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.
## Solution
### Method 1 – DataFrame Shape Property
#### Intuition
The shape property of a pandas DataFrame returns a tuple with the number of rows and columns. This is the most direct and efficient way to get the size of a DataFrame.
#### Approach
1. Use the `.shape` attribute of the DataFrame to get a tuple `(rows, columns)`.
2. Convert the tuple to a list as required by the problem statement.
3. Return the list.
#### Code
{{< code_tabs >}}
##### Python (pandas)
```python
class Solution:
def getDataframeSize(self, players) -> list[int]:
return list(players.shape)
Complexity
- ⏰ Time complexity:
O(1)— Accessing the shape property is a constant time operation. - 🧺 Space complexity:
O(1)— Only a fixed-size list is returned.