我有 2 个不同的数据框。 第一个看起来像:
joint label x z y pt
0 1 NaN 50.4 0.0 -8.40 10
1 2 shell 52.2 0.0 -8.40 20
2 3 shell 54.0 0.0 -8.40 30
3 4 shell 55.8 0.0 -8.40 40
4 5 shell 57.6 0.0 -8.40 50
我的第二个数据框看起来像:
member joint1 joint2 joint1_pt joint2_pt
0 1 1 2 0 0
1 2 2 3 0 0
2 3 3 4 0 0
3 4 4 5 0 0
我想使用对应于特定关节的 pt 值 并将其用于第二个数据帧,因此它看起来如下所示:
member joint1 joint2 joint1_pt joint2_pt
0 1 1 2 10 20
1 2 2 3 20 30
2 3 3 4 30 40
3 4 4 5 40 50
你能帮我举个例子/我应该如何处理这个问题吗? 提前致谢!!
最佳答案
你需要map
由 dict
创建自 Series
与 set_index
和 to_dict
正如 P-robot 中指出的那样在评论中:
d = df1.set_index('joint')['pt'].to_dict()
#mapping by Series works, but a bit slowier
#d = df1.set_index('joint')['pt']
print (d)
{1: 10, 2: 20, 3: 30, 4: 40, 5: 50}
df2['joint1_pt'] = df2['joint1'].map(d)
df2['joint2_pt'] = df2['joint2'].map(d)
print (df2)
member joint1 joint2 joint1_pt joint2_pt
0 1 1 2 10 20
1 2 2 3 20 30
2 3 3 4 30 40
3 4 4 5 40 50
关于python - Pandas dataframe 在不同的 dataframe 中查找一个值并赋值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42419268/