litovets
litovets

Reputation: 17

merge 2 pandas dataframe in 1 with data priority

I have 2 Dataframes:

A = [ A  B  C
name1 1  2  3
name2 1  2  3
name3 1  2  3
]

B = [  A  B  C  D
name1 11 12 13 12
name2 12 22 23 23
name3 31 32 33 14
name4 41 42 43 44
] 

I would like to add in A columns and rows that I don't have in A but have in B, without changing existing data in A

As a result, A must be:

A = [ A  B  C  D
name1 1  2  3  12
name2 1  2  3  23
name3 1  2  3  14
name4 41 42 43 44
]

How I should do it?

Upvotes: 1

Views: 629

Answers (2)

Piotr
Piotr

Reputation: 2117

You can use reindex followed by fillna:

index = A.index.union(B.index)
columns = A.columns.union(B.columns)
A.reindex(index=index, columns=columns).fillna(B).astype(int)

        A   B   C   D
name1   1   2   3  12
name2   1   2   3  23
name3   1   2   3  14
name4  41  42  43  44

Upvotes: 0

user3483203
user3483203

Reputation: 51155

Use combine_first

df1.combine_first(df2).astype(int)

        A   B   C   D
name1   1   2   3  12
name2   1   2   3  23
name3   1   2   3  14
name4  41  42  43  44

Upvotes: 4

Related Questions