문제
DataFrame df1
+-------------+--------+
| Column Name | Type |
+-------------+--------+
| student_id | int |
| name | object |
| age | int |
+-------------+--------+
DataFrame df2
+-------------+--------+
| Column Name | Type |
+-------------+--------+
| student_id | int |
| name | object |
| age | int |
+-------------+--------+
이 두 개의 DataFrame을 수직으로 하나의 DataFrame으로 결합하는 솔루션을 작성하세요. 결과 형식은 아래 예시와 같습니다.
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 |
+------------+---------+-----+
설명:
두 개의 DataFrame이 수직으로 쌓이고, 그들의 행이 결합됩니다.
풀이
import pandas as pd
def concatenateTables(df1: pd.DataFrame, df2: pd.DataFrame) -> pd.DataFrame:
df = pd.concat([df1, df2], axis=0)
return df
개념 정리
concat() ? 데이터 프레임을 합치는 함수
pd.concat([df1, df2], axis=0, ignore_index=True, join='inner')
- axis = 0/1 : 0은 행방향(위아래) / 1은 열방향(좌우)
- ignore_index = True : 이어붙이는 데이터프레임의 인덱스를 재배열하여 이어지게함
- join = 'inner' : default 는 outer(합집합)으로 설정 / inner(교집합)
'[코딩테스트] > [Pandas]' 카테고리의 다른 글
[LeetCode - Pandas] (Easy) 2886. Change Data Type (0) | 2025.03.28 |
---|---|
[LeetCode - Pandas] (Easy) 2887. Fill Missing Data (0) | 2025.03.28 |
[LeetCode - Pandas] (Easy) 2889. Reshape Data: Pivot (0) | 2025.03.27 |
[LeetCode - Pandas] (Easy) 2890. Reshape Data: Melt (0) | 2025.03.26 |
[LeetCode - Pandas] (Easy) 2891. Method Chaining (0) | 2025.03.26 |