LeetCode 2878.获取DataFrame的大小

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属性,调用时返回一个元组(number of rows, number of columns),将其转换为列表即可:

cpp 复制代码
import pandas as pd

def getDataframeSize(players: pd.DataFrame) -> List[int]:
    return [players.shape[0], players.shape[1]]
相关推荐
HjhIron10 小时前
面试常客:字符串算法从入门到进阶
算法·面试
吴佳浩12 小时前
DeepSeek DSpark:Confidence-Scheduled Speculative Decoding 技术解析
人工智能·算法·deepseek
触底反弹13 小时前
🧠 搞懂 Token,才算真正入门大模型——从分词原理到 Embedding 语义实战
javascript·人工智能·算法
vivo互联网技术17 小时前
ICLR 2026 | 基于后验采样的图像恢复方法LearnIR:人脸去阴影、去雾
人工智能·算法·aigc
浮生望19 小时前
JS字符串与回文算法:从包装类到双指针的面试进阶之路
javascript·算法
黄敬峰19 小时前
面试必刷:从JS底层包装类到双指针,彻底搞懂字符串与回文算法
算法
地平线开发者1 天前
J6B vio scenario sample
算法
BothSavage2 天前
Trae远程开发中DeepSeek自定义模型4054错误的排查与修复
算法
小林ixn2 天前
从暴力到KMP:一道题彻底搞懂字符串匹配的前世今生
算法