gianlucazani
gianlucazani

Reputation: 35

Union of two DataFrames with different column and row indexes updating existing values - Pandas

First of all I'll make an example of the result I want to obtain. I initially have two DataFrames with, in general different column indexes and row indexes and eventually with different rows and columns number (even if in the example below are both 3x3):

    Dataframe1 |     Dataframe2
    A   B   C  |      B   D   F
A   x   x   x  |  A   y   y   y
D   x   x   x  |  B   y   y   y
E   x   x   x  |  E   y   y   y

And I want the following result:

    Result
   A   B   C   D   F
A  x   y   x   y   y
B  -   y   -   y   y
D  x   x   x   -   -
E  x   y   x   y   y

Note the solution has the following characteristics:

My questions are:

Thank you.

Upvotes: 0

Views: 1129

Answers (2)

ziying35
ziying35

Reputation: 1305

try another:

cols = df1.columns.append(df2.columns).unique().sort_values()
idx = df1.index.append(df2.index).unique().sort_values()
res = df1.reindex(index=idx, columns=cols)
res.update(df2)
print(res)
>>>
    A   B   C   D   F
A   x   y   x   y   y
B   NaN y   NaN y   y
D   x   x   x   NaN NaN
E   x   y   x   y   y

Upvotes: 0

ziying35
ziying35

Reputation: 1305

try this:

data1 = {'A': {'A': 'x', 'D': 'x', 'E': 'x'},
         'B': {'A': 'x', 'D': 'x', 'E': 'x'},
         'C': {'A': 'x', 'D': 'x', 'E': 'x'}}
df1 = pd.DataFrame(data1)
print(df1)
>>>
    A   B   C
A   x   x   x
D   x   x   x
E   x   x   x

data2 = {'B': {'A': 'y', 'B': 'y', 'E': 'y'},
         'D': {'A': 'y', 'B': 'y', 'E': 'y'},
         'F': {'A': 'y', 'B': 'y', 'E': 'y'}}
df2 = pd.DataFrame(data2)
print(df2)
>>>
    B   D   F
A   y   y   y
B   y   y   y
E   y   y   y

res = df1.combine_first(df2)
print(res)
>>>
    A   B   C   D   F
A   x   y   x   y   y
B   NaN y   NaN y   y
D   x   x   x   NaN NaN
E   x   y   x   y   y

Upvotes: 2

Related Questions