Reshape Data: Concatenate — LeetCode 2888 Python Solution

EasyPandas
Problem
#2888
Reading time
2 min

Table schema

SQL
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

SQL
DataFrame df1
+-------------+--------+
| Column Name | Type   |
+-------------+--------+
| student_id  | int    |
| name        | object |
| age         | int    |
+-------------+--------+

DataFrame df2
+-------------+--------+
| Column Name | Type   |
+-------------+--------+
| student_id  | int    |
| name        | object |
| age         | int    |
+-------------+--------+

Python solution

Python
import pandas as pd


def concatenateTables(df1: pd.DataFrame, df2: pd.DataFrame) -> pd.DataFrame:
    return pd.concat([df1, df2], ignore_index=True)

Complexity

MeasureComplexity
TimeO(n)
SpaceO(1) to O(n) auxiliary

Related problems

Frequently asked questions

How hard is LeetCode 2888. Reshape Data: Concatenate?
LeetCode 2888. Reshape Data: Concatenate is rated Easy on LeetCode.
What topics does LeetCode 2888. Reshape Data: Concatenate cover?
LeetCode 2888. Reshape Data: Concatenate is tagged Pandas on LeetCode.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity — invisible to screen sharing.

Get Stealth Interview