What is the difference between join and merge in Pandas?

后端 未结 7 2330
没有蜡笔的小新
没有蜡笔的小新 2020-11-27 09:20

Suppose I have two DataFrames like so:

left = pd.DataFrame({\'key1\': [\'foo\', \'bar\'], \'lval\': [1, 2]})

right = pd.DataFrame({\'key2\': [\'foo\', \'bar         


        
7条回答
  •  天涯浪人
    2020-11-27 09:45

    From this documentation

    pandas provides a single function, merge, as the entry point for all standard database join operations between DataFrame objects:

    merge(left, right, how='inner', on=None, left_on=None, right_on=None,
          left_index=False, right_index=False, sort=True,
          suffixes=('_x', '_y'), copy=True, indicator=False)
    

    And :

    DataFrame.join is a convenient method for combining the columns of two potentially differently-indexed DataFrames into a single result DataFrame. Here is a very basic example: The data alignment here is on the indexes (row labels). This same behavior can be achieved using merge plus additional arguments instructing it to use the indexes:

    result = pd.merge(left, right, left_index=True, right_index=True,
    how='outer')
    

提交回复
热议问题