如果你的数据由多张表组成,那么有时候需要将不同的内容合并在一起分析
# 按照行索引进行
pd.concat([data, dummies], axis=1)
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']})
# 默认内连接
result = pd.merge(left, right, on=['key1', 'key2'])
左连接
result = pd.merge(left, right, how='left', on=['key1', 'key2'])
右连接
result = pd.merge(left, right, how='right', on=['key1', 'key2'])
外链接
result = pd.merge(left, right, how='outer', on=['key1', 'key2'])
3 总结