python - 无法连接两个数据帧

标签 python pandas dataframe

我想在 Pandas (python) 中连接两个数据帧 (df1, df2) 这样结果将包含 df1 的所有索引(唯一的和公共(public)的)以及 df2 具有但 df1 中不存在的任何内容。一个例子:

df1

     col1 col2
0     1   2
1     2   3
2     3   4

df2

     col1 col2
1     4   6
2     2   3
3     5   5

我希望结果是这样的:

     col1 col2
0     1   2
1     2   3
2     3   4
3     5   5

最佳答案

使用combine_first .

df3 = df1.combine_first(df2)

print(df3)

产量

   col1  col2
0     1     2
1     2     3
2     3     4
3     5     5

关于python - 无法连接两个数据帧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35729853/

相关文章:

Python:DataFrame.melt - 如何选择一系列列作为标识符变量?

python-3.x - Pandas 分组并计算两列的比率

python - 如何将两个非常大的字符串压缩在一起并返回匹配和不匹配的索引?

python - 在 python 中使用 xpath 查询从具有子节点的以下节点中选择整个文本

python - Pycharm 和 tensorflow GPU;未找到 DLL

python - 将类映射到 Pandas 一种热门编码

python - 当不重复跨越列标题时,如何将 csv 文件作为 MultiIndexed DataFrame 读取?

python - 使用另一个 DataFrame 从 Python 中的 DataFrame 中删除记录

python - 使用字符串和数字生成列名

python - 使用行和列值插入缺失值