将具有重叠索引但不重叠值的Pandas DataFrame连接起来 [英] Concatenating Pandas DataFrames with overlapping indexes but never overlapping values

查看:58
本文介绍了将具有重叠索引但不重叠值的Pandas DataFrame连接起来的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个任意形状的DataFrame:

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

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

两个DataFrame具有重叠的索引。在有重叠的地方,对于给定的列,在一个DataFrame中有一个非 NaN 和一个 NaN 在另一个。如何将它们连接起来,以便可以实现具有所有值且没有<$ c $ NaN s的DataFrame:

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

我建议的解决方案是:

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.

推荐答案

使用 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

这篇关于将具有重叠索引但不重叠值的Pandas DataFrame连接起来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆