0

here are 2 dataframes :

d1 = pd.DataFrame({'a': [19, 65, 7, 49, 66, 89, 545],
                  'b': [4, 6, 6, 90, 5, 77, 767],
                  'c': [34, 78, 65, 666, '', '', '']})

d2 = pd.DataFrame({'c': [34, 78, 65, '', ''],
                  'd': [4, 6, 6, 90, 767]})

I would like to make a merge between them with "c" column as jointure.

In my case, I use this :

df = pd.merge(d1, d2, how='left')

But the result is not good. In fact, I have some doublons, plus the final result should be a dataframe with the same length of d1. In my case It is not true.

Here is the result I would like to have :

df = pd.DataFrame({'a': [19, 65, 7, 49, 66, 89, 545],
                  'b': [4, 6, 6, 90, 5, 77, 767],
                  'c': [34, 78, 65, 666, '', '', ''],
                  'd': [4, 6, 6, 90, 767, '', '']})
ahmedaao
  • 377
  • 1
  • 3
  • 14
  • So, _how_ do you want to merge them? There are 5 ways of merging dataframes: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.merge.html#pandas.DataFrame.merge – ForceBru Jun 28 '21 at 16:10
  • I want to join the two dataframe on "c" column without duplicate row – ahmedaao Jun 28 '21 at 16:13
  • 1
    could you include your expected result? – Onyambu Jun 28 '21 at 16:16
  • Does this answer your question? [Pandas Merging 101](https://stackoverflow.com/questions/53645882/pandas-merging-101) – Amit Gupta Jun 28 '21 at 17:29

1 Answers1

2

IIUC:

use concat() and fillna():

df=pd.concat([d1,d2.pop('d')],axis=1).fillna('')
#OR
df=pd.concat([d1,d2['d']],axis=1).fillna('')

Now If you print df you will get your expected output

Anurag Dabas
  • 23,866
  • 9
  • 21
  • 41