pandas表连接 索引上的合并方法
如下所示:
left1 = pd.DataFrame({‘key':[‘a','b','a','a','b','c'],'value':range(6)}) right1 = pd.DataFrame({‘group_val':[3.5,7]},index = [‘a','b']) print(left1) print(right1) result = pd.merge(left1,right1,left_on='key',right_index=True) print(result)
层次化数据的索引
lefth = pd.DataFrame({‘key1':[‘Ohio','Ohio','Ohio','Nevada','Nevada'], ‘key2':[2000,2001,2002,2001,2002], ‘data':np.arange(5)}) print(lefth) righth = pd.DataFrame(np.arange(12).reshape(6,2),index = [[‘Nevada','Nevada','Ohio','Ohio','Ohio','Ohio'], [2001,2000,2000,200,2001,2002]]) print(righth) result = pd.merge(lefth,righth,left_on=[‘key1','key2'],right_index=True) print(result)
以上代码如果想改为外部连接 how = ‘outer' 就可以了
同时合并双方索引
left2 = pd.DataFrame([[1,2],[3,4],[5,6]],index=[‘a','c','e'],columns=[‘Ohio','Neveda']) right2 = pd.DataFrame([[7,8],[9,10],[11,12],[13,14]],index=[‘b','c','d','e'],columns=[‘Missouri','Alabma']) print(left2) print(right2) result = pd.merge(left2,right2,how='outer',left_index=True,right_index=True) print(result)
相关推荐
jzlixiao 2020-08-18
QianYanDai 2020-07-04
ribavnu 2020-11-16
moyekongling 2020-11-13
坚持是一种品质 2020-11-16
chenjiazhu 2020-09-29
kikaylee 2020-10-31
Ida 2020-09-16
liuweiq 2020-09-09
silencehgt 2020-09-07
sunnyxuebuhui 2020-09-07
西瓜皮儿的皮儿 2020-09-07
LuckyLXG 2020-09-08
明瞳 2020-08-19
MissFuTT 2020-08-18
zhushenghan 2020-08-16
罗罗 2020-08-16
mrandy 2020-08-15