LevDavidovich
LevDavidovich

Reputation: 97

Concatenating Pandas DataFrames with overlapping indexes but never overlapping values

I have two DataFrames of arbitrary shape of the type:

   A     B    C
0  A0   B0   C0
1  A1   B1   C1
2  A2   B2  NaN
3  A3  NaN  NaN
4  A4  NaN  NaN

and

     A    B   C
2  NaN  NaN  C2
3  NaN   B3  C3
4  NaN   B4  C4
5   A5   B5  C5
6   A6   B6  C6

The two DataFrames have overlapping indexes. Where there is an overlap, for a given column, there is a non-NaN in one DataFrame, and a NaN in the other. How can I concatenate these such that I can achieve a DataFrame with all values and no NaNs:

    A    B    C
0  A0   B0   C0
1  A1   B1   C1
2  A2   B2   C2
3  A3   B3   C3
4  A4   B4   C4
5  A5   B5   C5
6  A6   B6   C6

My proposed solution is:

df3 = pd.concat([pd.concat([df1[col].dropna(), df2[col].dropna()]) for col in df1.columns], axis=1)

However, ideally I would not work column-by-column.

Upvotes: 4

Views: 7575

Answers (2)

anky
anky

Reputation: 75080

using df.fillna() and df.append() with dropna()

df1.fillna(df2).append(df2).dropna()

    A   B   C
0   A0  B0  C0
1   A1  B1  C1
2   A2  B2  C2
3   A3  B3  C3
4   A4  B4  C4
5   A5  B5  C5
6   A6  B6  C6

Upvotes: 1

Space Impact
Space Impact

Reputation: 13255

Use combine_first:

df = df1.combine_first(df2)

print(df)
    A   B   C
0  A0  B0  C0
1  A1  B1  C1
2  A2  B2  C2
3  A3  B3  C3
4  A4  B4  C4
5  A5  B5  C5
6  A6  B6  C6

Upvotes: 13

Related Questions