user2913891
user2913891

Reputation: 37

concat overlapping data with pandas

I'm working on merging 2 data frames together that are all structured in the same fashion:

df1:

DATE   TANK#1 TANK#2 TANK#3 TANK#4 ... TANK#100
9/22   10     30     22     13         62
10/19  5      29     17     13         51

df2:

DATE   TANK#3 TANK#4 TANK#5 TANK#6 ... TANK#120
11/17  10     30     22     13         62
11/29  5      29     17     13         51

df1 has the date range 9/22/16 - 10/19/16

df2 has the date range 10/24/17 - 05/28/17

I would like to concat these DataFrames together so all of the dates are in one DataFrame. However, the column headers/tanks are not all the same for each data frame. It is mostly overlap, but some tanks exist in one and not in the other.

I tried

result = df1.append(df2)

But receive the error: Plan shapes are not aligned

What is the best way to merge data shaped like this?

Upvotes: 0

Views: 649

Answers (1)

Allen Qin
Allen Qin

Reputation: 19947

Set 'DATE' as index and combine df1 and df2 using combine_first.

df1.set_index('DATE').combine_first(df2.set_index('DATE')).reset_index()

Upvotes: 1

Related Questions