Python 在不同的数据帧中查找一个值并指定一个值

Python 在不同的数据帧中查找一个值并指定一个值,python,python-3.x,pandas,dataframe,Python,Python 3.x,Pandas,Dataframe,我有两个不同的数据帧。 第一个看起来像: 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

我有两个不同的数据帧。 第一个看起来像:

     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
我的第二个数据帧看起来像:

     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
我想使用特定joint上对应的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
你能帮我举个例子/想法说明我该如何处理这个问题吗? 提前谢谢你

合并后,您可以使用将pt分配给joint1\u pt和joint2\u pt,最后删除不需要的列

df= pd.merge(df2,df1[['joint','pt']], right_on='joint',left_on='joint1',how='left')
df= pd.merge(df,df1[['joint','pt']], right_on='joint',left_on='joint2',how='left')
df[['joint1_pt','joint2_pt']] =df[['pt_x','pt_y']] 
df=df[['member','joint1','joint2','joint1_pt','joint2_pt']]
print df
输出

   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
您需要通过
dict
Series
创建,并在注释中指出:

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

您可以使用
pd.DataFrame.to dict
(like)和
pd.Series.map
(like)。我尝试实现您的代码,我在joint1\u pt和joint2\u pt列scheck数据类型中得到NaN,可能需要转换为str或int,如
df2['joint1\u pt']=df2['joint1'].astype(str.map(d)
df2['joint1\u pt']=df2['joint1'.astype(int).map)(d) 
因为需要相同的。在dict和joint1和join2列中相同。您知道为什么在我的例子中,代码的第一行返回dict键的字符串类型吗?作为第二行,我添加这一行
d={int(k):int(v)表示k,v表示d.items()}
它现在可以工作了。但奇怪的是,为什么在你的密钥中返回整数类型。非常感谢!!df1['joint','pt']似乎不起作用。我得到一个密钥错误:('joint','pt')。即使我尝试打印它也不起作用。但当我尝试单独打印它们时,它会起作用。有什么想法吗?谢谢你的帮助!它不会给出错误,但你知道为什么它会为joint1\u pt和joint2\u pt列中的所有条目返回NaN吗?对于示例数据,它工作正常,可能是“joint”值与joint1和joint2不匹配在这些情况下可能是Nan