■ merge 함수의 left_on/right_index 인자를 사용해 INNER 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 45 46 47 48 49 50 51 52 53 54 55 56 |
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 -0.874219 1 B 1.437653 2 C 0.561296 3 D -0.169027 """ print() print(dataFrame2) """ key value 0 B 0.382614 1 D -0.157619 2 D 0.047836 3 E 1.650731 """ print() dataFrame3 = dataFrame2.set_index("key") print(dataFrame3) """ value key B 0.382614 D -0.157619 D 0.047836 E 1.650731 """ print() dataFrame4 = pd.merge(dataFrame1, dataFrame3, left_on = "key", right_index = True) # INNER JOIN print(dataFrame4) """ key value_x value_y 1 B 1.437653 0.382614 3 D -0.169027 -0.157619 3 D -0.169027 0.047836 """ |
▶ 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 명령을 실행했다.