1、從合并的方式看merge和join是一樣的,有l(wèi)eft/right/inner/outer,而concat只有inner/outer兩種,因?yàn)閙erge和join參與合并的對(duì)象有左右區(qū)分,而concat第一個(gè)參數(shù)是多個(gè)dataframe組成的列表,沒有嚴(yán)格的左右區(qū)分,如果排除最后結(jié)果中列的順序的話,可以看成是一樣的。例:
import pandas as pd
from pandas import Series,DataFrame,Panel
df1 = DataFrame([['a','b'],['d','q'],['o','b'],['m','e']],index=['a','b','c','o'],columns=['number1','number2'])
df2 = DataFrame([['a','b'],['d','e'],['a','b'],['d','e']],columns=['col1','col2'],index=['f','a','g','c'])
print pd.concat([df1,df2],join='outer',axis=1)
print pd.concat([df2,df1],join='outer',axis=1)
2、merge合并的范圍最廣泛,可以合并左邊對(duì)象的索引/列和右邊對(duì)象的索引/列的四種組合;join次之,僅可以實(shí)現(xiàn)調(diào)用DataFrame的索引/列和參數(shù)DataFrame的索引的合并,也就是參數(shù)DataFrame的列不能參與合并;concat合并的范圍最小,只支持索引的合并,也就是說索引與索引的合并是三個(gè)函數(shù)共同的功能,例:
import pandas as pd
import numpy as np
from pandas import Series,DataFrame,Panel
df1 = DataFrame([['a','b'],['d','q'],['o','b'],['m','e']],index=['a','b','c','o'],columns=['number1','number2'])
df2 = DataFrame([['a','b'],['d','e'],['a','b'],['d','e']],columns=['col1','col2'],index=['f','a','g','c'])
print(pd.merge(df1,df2,left_index=True,right_index=True,how='outer'))
print(df1.join(df2,how='outer'))
print(pd.concat([df1,df2],join='outer',axis=1))
可以看出三個(gè)函數(shù)合并索引后輸出的結(jié)果是一樣的
number1number2col1col2a a b d eb d q NaN NaNc o b d ef NaN NaN a bg NaN NaN a bo m e NaN NaN
number1number2col1col2a a b d eb d q NaN NaNc o b d ef NaN NaN a bg NaN NaN a bo m e NaN NaN
number1number2col1col2a a b d eb d q NaN NaNc o b d ef NaN NaN a bg NaN NaN a bo m e NaN NaN
3、concat和join的共同點(diǎn)是可以合并3個(gè)以上的對(duì)象,merge只合并兩個(gè)對(duì)象
-
merge
+關(guān)注
關(guān)注
0文章
5瀏覽量
2482 -
Join
+關(guān)注
關(guān)注
0文章
9瀏覽量
3260 -
concat
+關(guān)注
關(guān)注
0文章
3瀏覽量
1924
原文標(biāo)題:merge、join和concat三者的異同點(diǎn)
文章出處:【微信號(hào):gh_290ec34a3129,微信公眾號(hào):深度學(xué)習(xí)小王子】歡迎添加關(guān)注!文章轉(zhuǎn)載請(qǐng)注明出處。
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
評(píng)論