Skip to content

2888. Reshape Data: Concatenate

DataFrame df1

Column NameType
student_idint
nameobject
ageint

DataFrame df2

Column NameType
student_idint
nameobject
ageint

Instructions

  • Write a solution to concatenate these two DataFrames vertically into one DataFrame.
  • The result format is in the following example.

Example

Input:

DataFrame df1

student_idnameage
1Mason8
2Ava6
3Taylor15
4Georgia17

DataFrame df2

student_idnameage
5Leo7
6Alex7

Output:

student_idnameage
1Mason8
2Ava6
3Taylor15
4Georgia17
5Leo7
6Alex7

Explanation:

The two DataFramess are stacked vertically, and their rows are combined.

Submissions

python
import pandas as pd

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

Explanation

Python (Pandas)
Submitted by @noeyislearning
  • import pandas as pd: Import the pandas library to work with DataFrames.
  • def concatenateTables(df1: pd.DataFrame, df2: pd.DataFrame) -> pd.DataFrame: Define a function that takes two DataFrames as input and returns a concatenated DataFrame.
  • return pd.concat([df1, df2], axis=0): Concatenate the two DataFrames df1 and df2 vertically using the pd.concat function with axis=0.