Pandas数据帧在不同的数据帧中查找值并分配值

Gio*_*tos 2 python dataframe python-3.x pandas

我有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
Run Code Online (Sandbox Code Playgroud)

我的第二个数据框看起来像:

     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
Run Code Online (Sandbox Code Playgroud)

我想使用在特定连接上对应的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
Run Code Online (Sandbox Code Playgroud)

你能帮我一个关于如何处理这个问题的例子/想法吗?先感谢您!!

jez*_*ael 6

你需要map通过dict从创建Seriesset_indexto_dict为指向P-机器人的评论:

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
Run Code Online (Sandbox Code Playgroud)