■ merge 함수에사 on/how 인자를 사용해 LEFT OUTER JOIN 데이터를 만드는 방법을 보여준다.
▶ main.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
import pandas as pd import numpy as np dataFrame1 = pd.DataFrame({"key" : ["A", "B", "C", "D"], "value" : np.random.randn(4)}) dataFrame2 = pd.DataFrame({"key" : ["B", "D", "D", "E"], "value" : np.random.randn(4)}) print(dataFrame1) """ key value 0 A -1.241331 1 B -0.767490 2 C 2.178568 3 D 0.576111 """ print() print(dataFrame2) """ key value 0 B -0.947082 1 D -0.126260 2 D 1.114855 3 E 0.106320 """ print() dataFrame3 = pd.merge(dataFrame1, dataFrame2, on = "key", how = "left") # LEFT OUTER JOIN print(dataFrame3) """ key value_x value_y 0 A -1.241331 NaN 1 B -0.767490 -0.947082 2 C 2.178568 NaN 3 D 0.576111 -0.126260 4 D 0.576111 1.114855 """ |
▶ requirements.txt
1 2 3 4 5 6 7 8 |
numpy==2.1.2 pandas==2.2.3 python-dateutil==2.9.0.post0 pytz==2024.2 six==1.16.0 tzdata==2024.2 |
※ pip install pandas 명령을 실행했다.