2888. Reshape Data: Concatenate
DataFrame df1
Column Name | Type |
---|---|
student_id | int |
name | object |
age | int |
DataFrame df2
Column Name | Type |
---|---|
student_id | int |
name | object |
age | int |
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_id | name | age |
---|---|---|
1 | Mason | 8 |
2 | Ava | 6 |
3 | Taylor | 15 |
4 | Georgia | 17 |
DataFrame 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.
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 DataFramesdf1
anddf2
vertically using thepd.concat
function withaxis=0
.