#2878

Get the Size of a DataFrame

Easy
ArrayDataFrame Manipulation
LeetCode ↗

Approaches

Brute ForceOptimal
Complexity Comparison
Brute ForceOptimal Solution
Time
O(n²)
O(1)
Space
O(1)
O(1)
💡

Intuition

Time O(1)Space O(1)

Using built-in functions in pandas allows us to efficiently retrieve the size of the DataFrame without manual counting.

⚙️

Algorithm

2 steps
  1. 1Step 1: Use the shape attribute of the DataFrame to get dimensions.
  2. 2Step 2: Return the shape as an array.
solution.py14 lines
1# Full working Python code
2import pandas as pd
3
4def get_dataframe_size(players):
5    return players.shape.tolist()
6
7# Example usage
8players = pd.DataFrame({
9    'player_id': [846, 749, 155, 583, 388, 883, 355, 247, 761, 642],
10    'name': ['Mason', 'Riley', 'Bob', 'Isabella', 'Zachary', 'Ava', 'Violet', 'Thomas', 'Jack', 'Charlie'],
11    'age': [21, 30, 28, 32, 24, 23, 18, 27, 33, 36],
12    'position': ['Forward', 'Winger', 'Striker', 'Goalkeeper', 'Midfielder', 'Defender', 'Striker', 'Striker', 'Midfielder', 'Center-back']
13})
14print(get_dataframe_size(players))

Complexity note: The time complexity is O(1) because we directly access the shape of the DataFrame, which is a constant time operation.

  • 1Using built-in functions can greatly simplify code and improve performance.
  • 2Understanding DataFrame properties like shape is crucial for efficient data manipulation.

Solutions and explanations are original Tejav content. Problem titles © LeetCode — use the LeetCode button above for the full problem statement.