vlookup相当于使用pandas连接2个表

时间:2015-06-18 02:34:31

标签: python join pandas dataframe vlookup

情境:

  • df1具有所有可能的拍卖ID值
  • df2具有可能的拍卖ID值的子集
  • df2包含拍卖中存在的对象ID(可以是0或正整数)

目标:

创建一个新的df1列" object_id"填充df2

中的相应值

实施例...

df1 = pd.DataFrame(columns=['auction_id'], data=[1,2,3,4,5,6,7,8,9])

auction_id
1  
2  
3  
4  
5  
6  
7  
8  
9  


df2 = pd.DataFrame({'auction_id':[1,2,4,6,7,8,9], 'object_id': [640,0,332,332,0,58,332]})

auction_id  object_id  
1             640  
2             0  
4             332  
6             332  
7             0  
8             58  
9             332  

到目前为止我的最大努力

使用vlookup in Pandas using join):

df1['object_id'] = df1.auction_id.map(df2.object_id)

哪个收益率:

df1  
auction_id  object_id
1             0  
2             332  
3             332  
4             0  
5             58  
6             332  
7             NaN  
8             NaN  
9             NaN  

1 个答案:

答案 0 :(得分:1)

您可以使用pandas库中的merge功能,如下所示:

import pandas as pd

df1 = pd.DataFrame(columns=['auction_id'], data=[1,2,3,4,5,6,7,8,9])    
df2 = pd.DataFrame({'auction_id':[1,2,4,6,7,8,9], 'object_id': [640,0,332,332,0,58,332]})    

new_df = pd.merge(df1,df2,how='left',on='auction_id')

您可以在此处阅读有关合并功能的信息:http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.merge.html