Python——把几个DataFrame合并成一个DataFrame
目录
1 横向合并——merge
result = pd.merge(left, right, on='key')
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'])
2 横向合并——join
result = left.join(right, on=key_or_keys) #共同指标的关键词
result = pd.merge(left, right, left_on=key_or_keys, right_index=True,how='left', sort=False)
result = left.join(right, on='key')
3 竖向合并——append
result = df1.append(df2)
4 竖向合并——concat
frames = [df1, df2, df3]
result = pd.concat(frames)