■ merge 함수에사 on/how 인자를 사용해 RIGHT 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 |
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.604546 1 B -1.037901 2 C -0.910113 3 D -0.925161 """ print() print(dataFrame2) """ key value 0 B -0.448881 1 D -0.173234 2 D 0.109069 3 E -0.766205 """ print() dataFrame3 = pd.merge(dataFrame1, dataFrame2, on = "key", how = "right") # RIGHT OUTER JOIN print(dataFrame3) """ key value_x value_y 0 B -1.037901 -0.448881 1 D -0.925161 -0.173234 2 D -0.925161 0.109069 3 E NaN -0.766205 """ |
▶ 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 명령을 실행했다.