2888. 重塑数据:连结
2888. 重塑数据:连结
题目
DataFrame
df1
+-------------+--------+ | Column Name | Type | +-------------+--------+ | student_id | int | | name | object | | age | int | +-------------+--------+
DataFrame
df2
+-------------+--------+ | Column Name | Type | +-------------+--------+ | student_id | int | | name | object | | age | int | +-------------+--------+
Write a solution to concatenate these two DataFrames vertically into one DataFrame.
The result format is in the following example.
Example 1:
Input:
df1
+------------+---------+-----+ | student_id | name | age | +------------+---------+-----+ | 1 | Mason | 8 | | 2 | Ava | 6 | | 3 | Taylor | 15 | | 4 | Georgia | 17 | +------------+---------+-----+
df2
+------------+------+-----+ | student_id | name | age | +------------+------+-----+ | 5 | Leo | 7 | | 6 | Alex | 7 | +------------+------+-----+
Output:
+------------+---------+-----+ | student_id | name | age | +------------+---------+-----+ | 1 | Mason | 8 | | 2 | Ava | 6 | | 3 | Taylor | 15 | | 4 | Georgia | 17 | | 5 | Leo | 7 | | 6 | Alex | 7 | +------------+---------+-----+
Explanation: The two DataFramess are stacked vertically, and their rows are combined.
题目大意
DataFrame
df1
+-------------+--------+ | Column Name | Type | +-------------+--------+ | student_id | int | | name | object | | age | int | +-------------+--------+
DataFrame
df2
+-------------+--------+ | Column Name | Type | +-------------+--------+ | student_id | int | | name | object | | age | int | +-------------+--------+
编写一个解决方案,将两个 DataFrames 垂直 连接成一个 DataFrame。
结果格式如下示例所示。
示例 1:
输入:
df1
+------------+---------+-----+ | student_id | name | age | +------------+---------+-----+ | 1 | Mason | 8 | | 2 | Ava | 6 | | 3 | Taylor | 15 | | 4 | Georgia | 17 | +------------+---------+-----+
df2
+------------+------+-----+ | student_id | name | age | +------------+------+-----+ | 5 | Leo | 7 | | 6 | Alex | 7 | +------------+------+-----+
输出:
+------------+---------+-----+ | student_id | name | age | +------------+---------+-----+ | 1 | Mason | 8 | | 2 | Ava | 6 | | 3 | Taylor | 15 | | 4 | Georgia | 17 | | 5 | Leo | 7 | | 6 | Alex | 7 | +------------+---------+-----+
解释: 两个 DataFrame 被垂直堆叠,它们的行被合并。
解题思路
- 在 Pandas 中,可以使用
pd.concat()
来将多个 DataFrame 进行拼接。 pd.concat([df1, df2])
会将df1
和df2
按照行拼接,生成一个新的 DataFrame。- 默认情况下,
pd.concat()
会根据行的索引对两个 DataFrame 进行拼接。如果需要按列拼接,可以使用axis=1
参数。 - 拼接后的 DataFrame 会包含
df1
和df2
的所有行,且会保留原来的列结构。 - 返回拼接后的 DataFrame。
复杂度分析
- 时间复杂度:
O(n1 + n2)
,其中n1
和n2
分别是df1
和df2
的行数。pd.concat()
需要遍历两个 DataFrame 的行并将它们合并。 - 空间复杂度:
O(n1 + n2)
,因为拼接后的 DataFrame 需要存储df1
和df2
中所有的行。
代码
import pandas as pd
def concatenateTables(df1: pd.DataFrame, df2: pd.DataFrame) -> pd.DataFrame:
return pd.concat([df1, df2])