Does merge order matter?

Question

In the context of this exercise, does merge order matter for Pandas dataframes?

Answer

Yes. Order of the merged dataframes will effect the order of the rows and columns of the merged dataframe.

When using the merge() method, it will preserve the order of the left keys.

For example,
df1.merge(df2).merge(df3)

will preserve the order of df1 when it is merged with df2, then this order will be preserved when finally merging with df3.

However, if order of the result dataframe actually does not matter to us, then the merge order will not matter. So,
df3.merge(df1).merge(df2)

will result in the same result, but in a different order than the previous example.

4 Likes