美文网首页我爱编程
pandas对两个及以上DataFrame进行join

pandas对两个及以上DataFrame进行join

作者: oyosc | 来源:发表于2017-07-08 14:47 被阅读0次

今天想使用pandas来模拟数据库多表关联的操作,但是文章中join只列出了两个表相关联,对于两个以上的没有任何说明,根据文档中的描述终于找到了方法,代码如下:
文档链接:pandas

import pandas as pd

caller = pd.DataFrame({'key': ['K0', 'K1', 'K2', 'K3', 'K4', 'K5'],'A': ['A0', 'A1', 'A2', 'A3', 'A4', 'A5']})

other = pd.DataFrame({'key': ['K0', 'K1', 'K2'], 'B': ['B0', 'B1', 'B2']})

other1 = pd.DataFrame({'key': ['K0','K1','K4'], 'C': ['C0', 'C1', 'C2']})

data = caller.set_index('key').join([other.set_index('key'),     
other1.set_index('key')],how="inner")

print(data)

结果图:

image.png

相关文章

网友评论

    本文标题:pandas对两个及以上DataFrame进行join

    本文链接:https://www.haomeiwen.com/subject/cftjhxtx.html