카테고리 없음

Pandas 합집합

automon 2023. 2. 15. 08:43

Pandas의 merge 메서드는 공통의 열이나 색인을 기준으로 두 개 이상의 데이터프레임을 하나로 결합할 수 있는 메서드입니다. merge 연산의 결과는 모든 열을 포함하는 새 데이터프레임이 되며, 매칭되는 행이 결합됩니다.

Pandas merge is a method that allows you to combine two or more dataframes into one based on common columns or indices. The result of the merge operation is a new dataframe that includes all the columns from both the source dataframes, with the matching rows combined.

 

import pandas as pd

# 두 개의 샘플 데이터프레임 생성
df1 = pd.DataFrame({'key': ['A', 'B', 'C', 'D'], 'value1': [1, 2, 3, 4]})
df2 = pd.DataFrame({'key': ['B', 'D', 'E', 'F'], 'value2': [5, 6, 7, 8]})

# 'key' 열을 기준으로 두 데이터프레임을 합침
df_merged = pd.merge(df1, df2, on='key')

# 합쳐진 데이터프레임 출력
print(df_merged)

key value1 value2

0 B 2 5

1 D 4 6