Python 3.x 使用第一列作为索引合并2个数据帧
如何使用第一列作为索引(index)合并两个数据帧,并用df2的值覆盖df1的值 我尝试了各种各样的变化,但似乎没有任何效果。我试过几个例子:Python 3.x 使用第一列作为索引合并2个数据帧,python-3.x,pandas,dataframe,Python 3.x,Pandas,Dataframe,如何使用第一列作为索引(index)合并两个数据帧,并用df2的值覆盖df1的值 我尝试了各种各样的变化,但似乎没有任何效果。我试过几个例子: df 1: Condition Currency Total Hours 0 Used USD 100 1 Used USD 75 2 Used USD 13 3 Used USD
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
尝试
更新:
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
输出:
df.update(df2)
print(df)
这根本不输出任何内容。@Arty它更新原始的df
。打印输出是打印(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