python列表中的vlookup
我有两个数据帧: df1中的列items_list包含一个items_id列表。我要做的是在df1中创建一个新列(“domain_list”),其中包含根据df2为items_列表中的每个项对应的domain_id列表python列表中的vlookup,python,pandas,list,vlookup,Python,Pandas,List,Vlookup,我有两个数据帧: df1中的列items_list包含一个items_id列表。我要做的是在df1中创建一个新列(“domain_list”),其中包含根据df2为items_列表中的每个项对应的domain_id列表 它就像excel中的vlookup。以以下数据帧为例: import pandas as pd df1 = pd.DataFrame({'item_id': [100,200,300], 'other': [1,2,3]}) df2 = pd.DataFrame({'item
它就像excel中的vlookup。以以下数据帧为例:
import pandas as pd
df1 = pd.DataFrame({'item_id': [100,200,300], 'other': [1,2,3]})
df2 = pd.DataFrame({'item_id': [100,300], 'other2': ['one', 'three']})
df1:
item_id other
0 100 1
1 200 2
2 300 3
item_id other2
0 100 one
1 300 three
df2:
item_id other
0 100 1
1 200 2
2 300 3
item_id other2
0 100 one
1 300 three
现在使用join
:
df1.join(df2.set_index('item_id'), on='item_id')
item_id other other2
0 100 1 one
1 200 2 NaN
2 300 3 three