繁体   English   中英

如何使用 python 在 excels(不同的列名)上进行 vlookup

[英]How to do vlookup on excels(different column names) using python

专家,我想在具有两个不同列名的两个 excel 和 output 列名之间执行 vlookup 也不同。

Lets take below example to understand scenario In source excel file1 I am having column name as "Computer name" at position A, In source excel file2 I am also having column name as "short" at position B. I want to perform vlookup(a excel 文件 1 的“计算机名”列与 excel 文件 2 的“短”列之间的一种 sql 左连接。 After vlookup i want to add output of vlookup in excel file1 as cloumn name as "CMDB crosscheck" which is at position B in screen print shown below of final output excel file. 请注意 rest excel file1 的所有列将保留在那里,只是新列将在 ZBF5906737 文件中的 position B 出现

来源 Excel 文件 1:

在此处输入图像描述

来源 Excel 文件 2: 在此处输入图像描述

Output excel 文件:

在此处输入图像描述

我正在使用下面的代码,但它不起作用。 请您提出建议

import pandas as pd
import numpy as np

avclient_workbook ="AV_Clients1.xlsx"
cmdb_workbook = "cmdb_all.xlsx"

output_workbook = "AVClientCMDBAll.xlsx"
df_avclient_workbook = pd.read_excel(avclient_workbook)
df_cmdb_workbook = pd.read_excel(cmdb_workbook)

#print(df_avclient_workbook.columns)
#print(df_cmdb_workbook.columns)
df_avclient_workbook.rename(columns={'Computer name':'short'}, inplace=True) #just trying to rename it
#not able to achive :(
df_3 = pd.merge(df_avclient_workbook, df_cmdb_workbook[['short', 'short']], on='short',how='left')
print(df_3)

所以伙计们,我终于用下面的代码完成了。 尽管我已经手动添加了列列表,但仍然找到了一种可以动态提供此列列表的方法。

import pandas as pd
import numpy as np
import warnings
warnings.filterwarnings('ignore')

avclient_workbook ="File1.xlsx"
cmdb_workbook = "File2.xlsx"

output_workbook = "File3.xlsx"
df_avclient_workbook = pd.read_excel(avclient_workbook)
df_cmdb_workbook = pd.read_excel(cmdb_workbook)

merged_dataset = pd.merge(df_avclient_workbook, df_cmdb_workbook, how='left',
                          left_on='Computer name',
                          right_on='short')
final_dataset = merged_dataset[df_avclient_workbook.columns]
final_dataset['CMDB crosscheck'] = merged_dataset['short']
#list of column names which i want to populate
final_dataset = final_dataset[["Computer name","CMDB crosscheck","Computer DNS name","IP address","User account","Management server","Group name","Vendor","Product name","Product version","FW policy","Definition version","Server definition","Delta time","Definition date","Delta range","Last connection (UTC)","Last connection range","Last AD connection","AV status","FW status","IPS status","ATP status","Agent GUID","Agent version","Scan engine","FW version","Hotfixes","OS name","OS version","Platform","Platform type","Architecture","Group path","Cloud / infra","Management proxy","Management proxy IP","ITSM name","ITSM priority","ITSM install status","ITSM responsible group","Tags","Excluded tags","Tree sorting","Last update (UTC)","Problem found","Problem description"]]

final_dataset.to_excel(output_workbook, index=False)

#writer.save()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM