Pandas 数据帧在不同的数据帧中查找一个值并赋值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42419268/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pandas dataframe lookup a value in different dataframe and assign a value
提问by Giorgos Synetos
I have 2 different dataframes. The first one looks like:
我有 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
and my second dataframe looks like:
我的第二个数据框看起来像:
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
I would like take use the pt valuethat corresponds on a specific jointe and use it on the second dataframe so it will look like the following:
我想使用与特定关节对应的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
can you please help me with an example/idear on how should i approach this? Thank you in advance!!
你能帮我举个例子/我应该如何解决这个问题吗?先感谢您!!
回答by jezrael
You need map
by dict
created from Series
with set_index
and to_dict
as pointed in P-robotin comments:
您需要map
通过dict
从Series
withset_index
和to_dict
as 在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
回答by Shijo
you can use merge, after merging ,assign pt to joint1_pt & joint2_pt , finally drop unwanted columns.
您可以使用merge,合并后,将 pt 分配给 joint1_pt & joint2_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
Output
输出
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