python - Python VLookup get output without merge
问题描述
I have 2 data sets,and I need to get the 'MAKE'
column based on the 'MODEL'
but the results column in a separate dataframe (Not merge with existing dataframe)
I could merge the data by using below code,
def vlookup_sample():
df1 = pd.read_excel('tb1.xlsx', sheet_name='Sheet1')
df2 = pd.read_excel('tb2.xlsx', sheet_name='Sheet1')
df = pd.merge(df1, df2[['MODEL', 'MAKE']], on='MODEL', how='left')
df["MAKE"].fillna("<n/a>", inplace=True)
print df
Dataset 1
ID MODEL REQUESTS ORDERS
1 Golf 123 4
2 Passat 34 5
3 Model 3 500 8
4 M3 5 0
Dataset2
MODEL TYPE MAKE
Golf Sedan Volkswagen
M3 Coupe BMW
Model 3 Sedan Tesla
Expected output:
MAKE
Volkswagen
Nan
Tesla
BMW
How do I get the result in to a separate data
解决方案
您可以使用以下方法进行相同的合并并MAKE
从合并输出中调用该列df.loc[]
:
new_df = df1.merge(df2,on='MODEL',how='left').loc[:,['MAKE']]
#pd.merge(df1, df2[['MODEL', 'MAKE']], on='MODEL', how='left').loc[:,['MAKE']]
推荐阅读
- azure - 如何获取 azure blob 的复制状态
- java - 在 JTextPane 中转换为大写或小写时,文本样式和格式正在发生变化
- mysql - 从 MYSQL 中删除超过 X 天的数据,Unix 时间以毫秒为单位
- php - 无法在 1.7.4.3 上删除产品组合
- ios - 可能丢失钥匙串访问权限
- javascript - 无法在 PDFjs 中为 pdf.worker.js 指定自定义路径
- angularjs - AngularJS 加载资源失败:服务器响应状态为 403 ()
- thingsboard - Thingboard:从内部数据库获取资产 ID
- ip - 如何在区块链上建立网站以防止其在某些国家/地区被封锁?
- function - 谷歌云功能和在谷歌云上的虚拟机上运行的网络服务有什么区别?