目标:
按Chapter关键字合并2个表,并且保存为genSum.csv
实现思路:
csv表中Name中先提取关键字copy到Chapter列。然后merger表2,最后保存为genSum.csv。
截图.png
实现代码:__author__ = 'cllea'import pandas as pdimport numpy as np
df = pd.read_csv("task.csv")#提取Name列s =df["Name"]#转为listlistName=s.tolist()#list#在list中修改字符串for i, v in enumerate(listName):
listName[i] = v.strip()[v.index(']')+2:v.index(']')+11]#print(listName)#list转为dataframedata = pd.DataFrame(listName,columns=['Chapter'])#print(data)#按列拼接dataframedfA=pd.concat([df,data],axis=1)#print(dfA)#合并dataframedfB = 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")
作者:applecai
链接:https://www.jianshu.com/p/1b95fb4bf033