We use cookies (including Google cookies) to personalize ads and analyze traffic. By continuing to use our site, you accept our Privacy Policy.

Get the Size of a DataFrame

Difficulty: Easy


Problem Description

Write a solution to calculate and display the number of rows and columns of a DataFrame named players. Return the result as an array in the format [number of rows, number of columns].


Key Insights

  • The problem requires determining the dimensions of a DataFrame, specifically the number of rows and columns.
  • A DataFrame is a common data structure used in data analysis that organizes data in a tabular format.
  • The solution involves accessing the properties of the DataFrame that provide the number of rows and columns.

Space and Time Complexity

Time Complexity: O(1) - Retrieving the number of rows and columns is a constant time operation. Space Complexity: O(1) - The output is a fixed-size array of two integers, regardless of the size of the DataFrame.


Solution

To solve this problem, we will leverage the built-in properties of the DataFrame data structure. Specifically, we will access the shape attribute, which returns a tuple representing the dimensions of the DataFrame. The first element of the tuple corresponds to the number of rows, and the second element corresponds to the number of columns. We will then return these values as an array.


Code Solutions

def get_dataframe_size(players):
    # Get the number of rows and columns from the DataFrame
    rows, cols = players.shape
    # Return the result as an array
    return [rows, cols]
← Back to All Questions