Python实现excel vlookup功能

目标:

按Chapter关键字合并2个表,并且保存为genSum.csv

实现思路:

csv表中Name中先提取关键字copy到Chapter列。然后merger表2,最后保存为genSum.csv。


截图.png

实现代码:

__author__ = 'cllea'
import pandas as pd
import numpy as np


df = pd.read_csv("task.csv")
#提取Name列
s =df["Name"]
#转为list
listName=s.tolist()#list
#在list中修改字符串
for i, v  in enumerate(listName):
        listName[i] = v.strip()[v.index(']')+2:v.index(']')+11]
#print(listName)
#list转为dataframe
data = pd.DataFrame(listName,columns=['Chapter'])
#print(data)
#按列拼接dataframe
dfA=pd.concat([df,data],axis=1)
#print(dfA)
#合并dataframe
dfB = pd.read_excel("myplan.xlsx")
#print(dfB)
#对关键字Chapter列向左连接(左边dfA为全部)
dfC = pd.merge(dfA, dfB,how='left',on=['Chapter'])
#print(dfC)
#保存到csv中
dfC.to_csv('genSum.csv',chunksize=10,encoding="utf_8_sig")

你可能感兴趣的:(Python实现excel vlookup功能)