当前位置: 首页 > news >正文

[每日一练]从表中创建DataFrame

该题目来源于力扣:

2877. 从表中创建 DataFrame - 力扣(LeetCode)icon-default.png?t=N7T8https://leetcode.cn/problems/create-a-dataframe-from-list/description/?lang=pythondata

题目要求:

编写一个解决方案,基于名为  student_data 的二维列表 创建 一个 DataFrame 。这个二维列表包含一些学生的 ID 和年龄信息。DataFrame 应该有两列, student_id 和 age,并且与原始二维列表的顺序相同。返回结果格式如下示例所示。示例 1:输入:
student_data:
[[1, 15],[2, 11],[3, 11],[4, 20]
]
输出:
+------------+-----+
| student_id | age |
+------------+-----+
| 1          | 15  |
| 2          | 11  |
| 3          | 11  |
| 4          | 20  |
+------------+-----+
解释:
基于 student_data 创建了一个 DataFrame,包含 student_id 和 age 两列。

思路流程:

该题目要求我们可以通过pandas将二维数据列表转换为一个具有二维数据结构的表格。

特殊代码:pd.Dataframe(data,index,columns,dtype,copy)

  1. 参数1,data:传入的字典/列表/二维数据集
  2. 参数2,index:指定的行索引
  3. 参数3,columns:指定的列索引
  4. 参数4,dtype:定义的数据类型
  5. 参数5,copy:TRUE表示原始数据和新 DataFrame 之间不会共享数据,修改 DataFrame 不会影响原始数据。如果为 False,则 DataFrame 可能会使用传入数据的引用。这意味着对 DataFrame 的修改可能会影响原始数据。

由此可见,该题目的要求很简答,我们只需要使用参数3,columns设定列索引即可。

代码实现:

import pandas as pddef createDataframe(student_data: List[List[int]]) -> pd.DataFrame:DataFrame_New=pd.DataFrame(student_data,columns=['student_id','age'])return DataFrame_New


http://www.mrgr.cn/news/12541.html

相关文章:

  • PHP MySQL 插入多条数据
  • 【Google Maps JavaScript API】Right-to-Left Languages 实现指南
  • 10款免费电脑录屏软件盘点,2024年最新录屏工具排行榜
  • 23种设计模式详解-创建模式篇
  • 功能测试理论
  • Linux小项目 迷你服务端实现在线商城
  • LeetCode49. 字母异位词分组(2024秋季每日一题 4)
  • STM32学习记录-05 -1-TIM定时中断
  • 【Liunx入门】Liunx软件包管理器
  • 【Qt】容器类控件TabWidget
  • 2-74 基于matlab的图像k-means聚类GUI
  • Spring核心概念复习AOP
  • 硬件调试经验积累 关于RTC 时钟问题。
  • TypeScript为何需要定义比较复杂的泛型类型?
  • Dockerfile应用、私有仓库
  • C#WinFrom 中实现可自定义按钮和事件的消息提示框
  • 最佳外推发帖器推荐
  • Rust: Reading and Writing Files
  • Akka-路由策略
  • 求解器的学习记录