您好,登錄后才能下訂單哦!
如下所示:
left1 = pd.DataFrame({‘key':[‘a(chǎn)','b','a','a','b','c'],'value':range(6)}) right1 = pd.DataFrame({‘group_val':[3.5,7]},index = [‘a(chǎn)','b']) print(left1) print(right1) result = pd.merge(left1,right1,left_on='key',right_index=True) print(result)
層次化數(shù)據(jù)的索引
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' 就可以了
同時(shí)合并雙方索引
left2 = pd.DataFrame([[1,2],[3,4],[5,6]],index=[‘a(chǎn)','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)
以上這篇pandas表連接 索引上的合并方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持億速云。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。