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, '', '']})
4
  • So, how do you want to merge them? There are 5 ways of merging dataframes: pandas.pydata.org/pandas-docs/stable/reference/api/… Commented Jun 28, 2021 at 16:10
  • I want to join the two dataframe on "c" column without duplicate row Commented Jun 28, 2021 at 16:13
  • 1
    could you include your expected result? Commented Jun 28, 2021 at 16:16
  • Does this answer your question? Pandas Merging 101 Commented Jun 28, 2021 at 17:29

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

4 Comments

It is not exactly the result I want. You can see it in the description
@ahmedaao why you are getting 867 at index 4 in your expected output?
sorry. Mistake It is 767
@ahmedaao Updated answer.....Kindly have a look :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.