LeetCode 题解工作台

从表中创建 DataFrame

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

category

0

题型

code_blocks

1

代码语言

hub

0

相关题

当前训练重点

简单 · create·a·dataframe·from·链表·core·interview·pattern

bolt

答案摘要

import pandas as pd def createDataframe(student_data: List[List[int]]) -> pd.DataFrame:

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 create·a·dataframe·from·链表·core·interview·pattern 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

编写一个解决方案,基于名为  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 两列。
lightbulb

解题思路

方法一

1
2
3
4
5
6
import pandas as pd


def createDataframe(student_data: List[List[int]]) -> pd.DataFrame:
    return pd.DataFrame(student_data, columns=['student_id', 'age'])
speed

复杂度分析

指标
时间Depends on the final approach
空间Depends on the final approach
psychology

面试官常问的追问

外企场景
  • question_mark

    Check if the candidate uses pandas efficiently.

  • question_mark

    Look for the candidate's attention to the specific format in the output.

  • question_mark

    Evaluate the clarity of the candidate's explanation of the solution.

warning

常见陷阱

外企场景
  • error

    Forgetting to explicitly name the columns in the DataFrame constructor.

  • error

    Overcomplicating the solution by manually iterating through the list when pandas provides a simpler method.

  • error

    Not ensuring that the format of the output matches the expected example.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Use a different dataset to verify that the solution works for other inputs.

  • arrow_right_alt

    Handle missing values or inconsistent data within the list and ensure the DataFrame creation still works.

  • arrow_right_alt

    Consider handling more complex column structures beyond just student_id and age.

help

常见问题

外企场景

从表中创建 DataFrame题解:create·a·dataframe·from… | LeetCode #2877 简单