电子说
1、从合并的方式看merge和join是一样的,有left/right/inner/outer,而concat只有inner/outer两种,因为merge和join参与合并的对象有左右区分,而concat第一个参数是多个dataframe组成的列表,没有严格的左右区分,如果排除最后结果中列的顺序的话,可以看成是一样的。例:
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合并的范围最广泛,可以合并左边对象的索引/列和右边对象的索引/列的四种组合;join次之,仅可以实现调用DataFrame的索引/列和参数DataFrame的索引的合并,也就是参数DataFrame的列不能参与合并;concat合并的范围最小,只支持索引的合并,也就是说索引与索引的合并是三个函数共同的功能,例:
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))
可以看出三个函数合并索引后输出的结果是一样的
number1 number2 col1 col2a a b d eb d q NaN NaNc o b d ef NaN NaN a bg NaN NaN a bo m e NaN NaN
number1 number2 col1 col2a a b d eb d q NaN NaNc o b d ef NaN NaN a bg NaN NaN a bo m e NaN NaN
number1 number2 col1 col2a 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的共同点是可以合并3个以上的对象,merge只合并两个对象
全部0条评论
快来发表一下你的评论吧 !