1

I am trying to combine dataframe_A:

file 1
file 2
file 3 
file 4 
file 5

with dataframe_B:

file 2 | some data  | more data
file 4 | other data | additional data
file 5 | data       | data data

along the file_name column, to end up with something like this:

file 1 |     ~      |      ~
file 2 | some data  | more data
file 3 |     ~      |      ~
file 4 | other data | additional data
file 5 | data       | data data

I want to end up with a dataframe the length of dataframe_A, with all the data from dataframe_B, and with blank/whatever in the intervening spaces.

the joins and merges I have tried so far just end up with something that looks like dataframe_B, which is not what I want. What do I need to do?

  • 3
    You're looking for left merge. `df3 = df1.merge(df2, how='left')` (Note: you may also need to specify column names but since no column names were provided I cannot say for sure) – Henry Ecker Nov 10 '21 at 20:50

1 Answers1

3

Use merge with how='left' parameter:

>>> dfA.merge(dfB, on='A', how='left').fillna('~')

        A           B                C
0  file 1           ~                ~
1  file 2   some data        more data
2  file 3           ~                ~
3  file 4  other data  additional data
4  file 5        data        data data

I recommend reading our extended introduction: Pandas Merging 101

Setup:

dfA = pd.DataFrame({'A': ['file 1', 'file 2', 'file 3', 'file 4', 'file 5']})

dfB = pd.DataFrame({'A': ['file 2', 'file 4', 'file 5'],
                    'B': ['some data', 'other data', 'data'],
                    'C': ['more data', 'additional data', 'data data']})
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Corralien
  • 109,409
  • 8
  • 28
  • 52
  • Please do not use inline code formatting for things that are not code, like question titles. It is not meant for emphasis, it is *only* for code. Use bold/italic for emphasis, if you really need to emphasize something. Also, please consider carefully whether it is actually necessary to post an answer when we have a canonical Q&A covering the subject already. You have the ability to single-handedly close questions as duplicates. This really helps in keeping the site clean and reducing the duplication of information. Please use it! Thanks. – Cody Gray - on strike Nov 18 '21 at 19:34