pandas merge 使用方法

import pandas as pd

left = pd.DataFrame({'key1': ['K0', 'K0', 'K1', 'K2'],
                     'key2': ['K0', 'K1', 'K0', 'K1'],
                     'A': ['A0', 'A1', 'A2', 'A3'],
                     'B': ['B0', 'B1', 'B2', 'B3']})

right = pd.DataFrame({'key1': ['K0', 'K1', 'K1', 'K2'],
                      'key2': ['K0', 'K0', 'K0', 'K0'],
                      'C': ['C0', 'C1', 'C2', 'C3'],
                      'D': ['D0', 'D1', 'D2', 'D3']})
print(left)
print(right)

left_merge1 = pd.merge(left=left,right=right,on=['key1'],how='left',suffixes=['_l', '_r'])
left_merge2 = pd.merge(left=left,right=right,on=['key1','key2'],how='left',suffixes=['_l', '_r'])
print(left_merge1)
print(left_merge2)

pandas merge 使用方法_第1张图片

你可能感兴趣的:(Python)