python(numpy,pandas11)——pandas merge根据索引合并数据
生活随笔
收集整理的這篇文章主要介紹了
python(numpy,pandas11)——pandas merge根据索引合并数据
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
文章目錄
- 前言
- 參數(shù)on
- 參數(shù) index
- 參數(shù)indicator
- 參數(shù) suffixes
前言
根據(jù) 莫煩Python的教程 總結(jié)寫成,以便自己復(fù)習(xí)和使用,這里我就不喲林地掛原創(chuàng)了🐶。
參數(shù)on
on:以列為索引進行合并
left = pd.DataFrame({'key':['K0','K1','K2','K3'],'A':['A0','A1','A2','A3'],'B':['B0','B1','B2','B3']})right = pd.DataFrame({'key':['K0','K1','K2','K3'],'C':['C0','C1','C2','C3'],'D':['D0','D1','D2','D3']}) res = pd.merge(left,right,on=['key1','key2'],how='left') # how = inner:黨同伐異;outer:求同存異;right:以第二個參數(shù)即right的'key1','key2'為準,得到的結(jié)果包含所有的'key1','key2'值,left:同理參數(shù) index
index:以行為索引進行合并
left = pd.DataFrame({'A':['A0','A1','A2'],'B':['B0','B1','B2']},index=['K0','K1','K2'])right = pd.DataFrame({'C':['C0','C1','C2'],'D':['D0','D1','D2']},index=['K0','K2','K3']) res = pd.merge(left,right,left_index=True,right_index=True,how='outer')參數(shù)indicator
indicator 顯示數(shù)據(jù)是按照哪種規(guī)則合成
df1 = pd.DataFrame({'col1':[0,1],'col_left':['a','b']}) df2 = pd.DataFrame({'col1':[1,2,2],'col_right':[2,2,2]}) res = pd.merge(df1,df2,on='col1',how='outer',indicator='indicator')參數(shù) suffixes
給合并后的數(shù)據(jù)加上后綴以區(qū)分不同的數(shù)據(jù)
boys = pd.DataFrame({'k':['K0','K1','K2'],'age':[1,2,3]})girls = pd.DataFrame({'k':['K0','K0','K3'],'age':[4,5,6]}) res = pd.merge(boys,girls,on='k',suffixes=['_boy','_girl'],how='inner')總結(jié)
以上是生活随笔為你收集整理的python(numpy,pandas11)——pandas merge根据索引合并数据的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python(numpy,pandas1
- 下一篇: python(numpy,pandas1