跳至主要內容

2878. 获取 DataFrame 的大小


2878. 获取 DataFrame 的大小

🟢   🔗 力扣open in new window LeetCodeopen in new window

题目

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.

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.

题目大意

DataFrame players:

+-------------+--------+
| Column Name | Type   |
+-------------+--------+
| player_id   | int    |
| name        | object |
| age         | int    |
| position    | object |
| ...         | ...    |
+-------------+--------+

编写一个解决方案,计算并显示 players行数和列数

将结果返回为一个数组:

[number of rows, number of columns]

返回结果格式如下示例所示。

示例 1:

输入:

+-----------+----------+-----+-------------+--------------------+
| 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            |
+-----------+----------+-----+-------------+--------------------+

输出:[10, 5]

解释:

这个 DataFrame 包含 10 行和 5 列。

解题思路

可以直接使用 DataFrame.shape 属性来获取行数和列数,并将其作为结果数组返回。

复杂度分析

  • 时间复杂度O(1),访问 DataFrame.shape 属性是一个恒定时间操作。
  • 空间复杂度O(1),只返回一个包含两个整数的数组。

代码

import pandas as pd

def getDataframeSize(players: pd.DataFrame) -> List[int]:
  return [players.shape[0], players.shape[1]]
  # OR
  return list(players.shape)