专家们,我想用两个不同的列名在两个excel之间执行vlookup,并且输出列名也要不同
让我们以下面的例子来理解这个场景 在源excel文件1中,我在位置A处的列名为“Computer name”,在源excel文件2中,我在位置B处的列名为“short”。我想在excel文件1的“Computer name”列和excel文件2的“short”列之间执行vlookup(sql的一种左连接)。在vlookup之后,我想在excel文件1中添加vlookup的输出,作为cloumn名称,作为“CMDB交叉检查”,它位于最终输出excel文件下面显示的屏幕打印的位置B。请注意,excel文件1的其余所有列将保留在那里,只有新列将出现在excel文件1中的位置B
源Excel文件1:
输出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)
伙计们,我终于用下面的代码完成了。虽然我已经手动添加了列列表,但仍然找到了一种方法,可以动态地给出这个列列表
相关问题 更多 >
编程相关推荐