python-3.x - 使用第一列作为索引合并 2 个数据框
问题描述
df 1:
Condition Currency Total Hours
0 Used USD 100
1 Used USD 75
2 Used USD 13
3 Used USD NaN
df 2:
Condition Currency Total Hours
1 Used USD 99
3 New USD 1000
Desired Result:
Condition Currency Total Hours
0 Used USD 100
1 Used USD 99
2 Used USD 13
3 New USD 1000
如何使用第一列作为索引(索引)合并两个数据帧,并用 df2 的值覆盖 df1 的值?
我尝试了各种变体,但似乎没有任何效果。我试过的几个例子:
pd.merge(df, df1) = result is an empty dataframe
df.combine_first(df1) = the result is a dataframe but with the same values as df1
解决方案
尝试update
:
df.update(df2)
print(df)
输出:
Condition Currency Total Hours
0 Used USD 100.0
1 Used USD 99.0
2 Used USD 13.0
3 New USD 1000.0
推荐阅读
- r - 如何访问由 ggplot2 中的 geom_text 绘制的标签的尺寸?
- python-2.7 - 在matlab中调用pytorch(python)模型时出现ImportError
- c - 将数据打印到不同的终端 IAR
- winapi - 如何使 WinInet HTTPS 后续请求重用连接?
- sql-server - 从 SQL Server 2008 迁移到 SQL Server 2016
- c# - 在 C# 中使用秒表
- php - 如何使用 curl 数组返回视图
- html - 将变量从 html 传递到服务器
- google-analytics - 在 Google Analytics 中,行为/事件/页面/报告中的“事件”和“网站使用”之间的关系是什么
- ubuntu - 如何将我的全局 vue-cli 安装升级到最新版本?