0

我有两个数据框,并试图根据两个条件工具和位置更新 DF1 中的名称-

数据框 -

第一

DF1

NAME    Tool    Location
-       tool_1  location_1
-       tool_15 location_2 
-       tool_19 location_3 

第二个 -

DF2

NAME    Tool    Location
name51  tool_1  location_1
name42  tool_15 location_2 
name33  tool_19 location_3

我尝试使用 numpy where 条件检查两个值,但是收到错误消息 -

ValueError:只能比较标签相同的系列对象

我知道问题是我的两个数据框中的行号不同。我尝试了一些重置索引的解决方案,但没有成功。

这是我尝试的查询-

DF1['NAME'] = np.where((DF1.Tool == DF2.Tool) & (DF1.Location== DF2.Location), DF2.Name)

有什么办法可以解决这个问题?我无法将两个数据框都与行的确切长度相匹配。

DF1 的预期结果是 -

DF1

NAME    Tool    Location
name51  tool_1  location_1
name42  tool_15 location_2 
name33  tool_19 location_3

谢谢你,

4

1 回答 1

1

合并和reindex()

DF1=DF1.merge(DF2,on=['Tool','Location'],suffixes=('_x','')).reindex(DF1.columns,axis=1)

     NAME     Tool    Location
0  name51   tool_1  location_1
1  name42  tool_15  location_2
2  name33  tool_19  location_3
于 2019-05-28T15:28:41.453 回答