Concatenating Pandas DataFrames with overlapping indexes but never overlapping values

本秂侑毒 提交于 2020-04-16 06:16:09

问题


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.


回答1:


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



回答2:


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


来源:https://stackoverflow.com/questions/54106361/concatenating-pandas-dataframes-with-overlapping-indexes-but-never-overlapping-v

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!