0

Let's say I have two data frame and I want to combine them (more like concatenating than merging since I am not joining on any columns). Here is a made-up example:

Here is table 1:

player_name   points  rebounds  assists
player1       27.0    9.0       9.0
player2       31.0    4.0       5.0
player3       15.0    10.0      1.0

Here is table 2:

player_name   points  height  weight
player4       17.0    6'0"    200
player5       21.0    6'2"    230
player6       26.0    6'5"    270

Here is the resulting table:

player_name   points  rebounds  assists  height  weight  
player1       27.0    9.0       9.0      Null    Null
player2       31.0    4.0       5.0      Null    Null
player3       15.0    10.0      1.0      Null    Null
player4       17.0    Null      Null     6'0"    200
player5       21.0    Null      Null     6'2"    230
player6       26.0    Null      Null     6'5"    270

Basically, both tables do not have the same columns, and that is okay. I want to retain all the columns from each of the tables (combining the ones in common, such as points in this case), and fill in Null values for the ones not in common. How can I do this simply in Python/Pandas? This is slightly different than a simple df join or concatenate, from my understanding.

Jane Sully
  • 3,137
  • 10
  • 48
  • 87
  • 4
    `pd.concat([df1, df2], axis=0)` https://pandas.pydata.org/pandas-docs/stable/generated/pandas.concat.html – BallpointBen May 31 '18 at 19:44
  • 1
    @BallpointBen Agreed! I think by default `axis=0`, `pd.concat([table1, table2], ignore_index=True)` – niraj May 31 '18 at 19:46
  • 1
    With pandas default arguments subject to change, I always like to specify the arguments whose behavior I rely on. – BallpointBen May 31 '18 at 19:47

0 Answers0