创建一个新的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
答案 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