0

I tried to merge two dataframes:

df1 contains columns a b c, row 1,2,3
df2 contains columns a b c, row 4,5,6

when using pd.merge(df1,df2), some of the row data gets erased and disappears from the merged df. Why?

smci
  • 32,567
  • 20
  • 113
  • 146
  • 1
    Show an [MCVE](https://stackoverflow.com/help/minimal-reproducible-example) as properly formatted code in the question. – Michael Butscher Jan 19 '22 at 07:36
  • 1
    Your question needs to be completed with a reproductible example: data and code. – hpchavaz Jan 19 '22 at 07:38
  • 1
    Take a look at [Pandas Merging 101](https://stackoverflow.com/q/53645882/15239951) – Corralien Jan 19 '22 at 07:51
  • `pd.merge(df1, df2)` is not what you expect. What do you want to do exactly, and please provide a [mre] with the expected output... – Serge Ballesta Jan 19 '22 at 09:19
  • `merge` is a SQL-style `join`. Not necessarily an all-inclusive merge of all rows; the name is a misnomer if you don't know how SQL-style works (`how=inner|(left|right|full) outer`). It's all explained in [Pandas Merging 101](https://stackoverflow.com/q/53645882/15239951) – smci Jan 19 '22 at 09:25

1 Answers1

1

You can try this: pd.concat([df1,df2])

It works.

Example:

df1 = pd.DataFrame(np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]),
                   columns=['a', 'b', 'c'])
df2 = pd.DataFrame(np.array([[11, 22, 33], [44, 55, 66], [77, 88, 99]]),
                   columns=['a', 'b', 'c'])
pd.concat([df1,df2],ignore_index=True)

You will get the table with all elements from two dataframes. ignore_index=True helps to avoid confused numeration of index.

Also you can use:

df1.merge(df2, how='outer')

You should check https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.merge.html

huanpops
  • 92
  • 5