1

I have two dataframes on which I would like to perform an outer join. Both dataframes share a common index name along with several column names that also share the same name.

I would like to combine these dataframes with an outer join on the index (no indices are lost but common indices combine). In addition, I also want to combine the columns that share the same name across both dataframes.

So far I have been able to do one or the other using merge(), join(), concat(). I have not yet been able to produce a dataframe that joins on the index while also combining the identical columns.

Example of the dataframes that I would like to combine:

df1 Looks like this; Index = 'Resource_Name':

RESOURCE_NAME  PROGRAM_NAME  CENTER STATUS
Doe, John            Prog 1   10545    ETW
Day, Jane            Prog 2   80942    FTE
Dylan, Bob           Prog 3   70641    ETW

df2 looks like this; Index = 'Resource_Name':

RESOURCE_NAME  PROGRAM_NAME  CENTER        MANAGER
Hobbs, Bobs          Prog 4   20813    Costas, Bob
Day, Jane            Prog 2   80942  Harlan, Kevin
Dylan, Bob           Prog 3   70641     Nance, Jim

Desired output:

RESOURCE_NAME   PROGRAM_NAME  CENTER        MANAGER  STATUS
Doe, John             Prog 1   10545            nan     ETW
Hobbs, Bobs           Prog 4   20813    Costas, Bob     nan
Day, Jane             Prog 2   80942  Harlan, Kevin     FTE
Dylan, Bob            Prog 3   70641     Nance, Jim     ETW

Any help would be much appreciated.

1 Answers1

2

Try combine_first:

df1.combine_first(df2)

Output:

                CENTER        MANAGER PROGRAM_NAME STATUS
RESOURCE_NAME                                            
Day, Jane      80942.0  Harlan, Kevin       Prog 2    FTE
Doe, John      10545.0            NaN       Prog 1    ETW
Dylan, Bob     70641.0     Nance, Jim       Prog 3    ETW
Hobbs, Bobs    20813.0    Costas, Bob       Prog 4    NaN
Scott Boston
  • 147,308
  • 15
  • 139
  • 187
  • 1
    Awesome! In eight hours of trying to figure out how to do this, I never came across that function. That was a huge help. Thank you! – The Chiefsus Dec 10 '19 at 23:39