每日leetcode

2878. 获取 DataFrame 的大小 - 力扣(LeetCode)

题目

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 列。

思路

  1. 通过df.shape函数获取行列,第0个元素为行数,第1个元素为列数。
  2. 但是shape的数据结构是元组,不是列表,所以我们要将它取出来放在列表中。

代码实现

import pandas as pd

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

知识积累

  • 前面忘记注意了,Python的函数编写的标准方式为:def 函数名(参数列表(参数名:参数类型)) -> 返回值类型: 函数实现
  • DataFrame是一个二维数据结构,可通过shape参数来获得——(行数,列数),是元组数据类型。
  • python的列表:[数据1, 数据2, ...]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值