用python(2.7) pandas处理excel多个sheet,各sheet之间有关联字段,起到的效果相当于多张RDB的表,做联表查询。
二、python代码import pandas as pdimport xlsxwriterdef print_hi(name): # Use a breakpoint in the code line below to deBUG your script. dfcity = \ pd.read_excel('/Users/xxxx/documents/excels/doctor_data.xlsx', sheet_name='city') dfarea = \ pd.read_excel('/Users/xxxx/documents/excels/doctor_data.xlsx', sheet_name='area') dfHospital = \ pd.read_excel('/Users/xxxx/documents/excels/doctor_data.xlsx', sheet_name='hospital') dfDepartment = \ pd.read_excel('/Users/xxxx/documents/excels/doctor_data.xlsx', sheet_name='department') dfDoctor = \ pd.read_excel('/Users/xxxx/documents/excels/doctor_data.xlsx', sheet_name='doctor') df1 = pd.merge(dfcity, dfarea, left_on='ID_city', right_on='cityID', how='inner') df2 = pd.merge(df1, dfHospital, left_on='ID_area', right_on='areaID', how='inner') df3 = pd.merge(df2, dfDepartment, left_on='ID_hospital', right_on='hospitalID', how='inner') df4 = pd.merge(df3, dfDoctor, left_on='ID_department', right_on='departmentID', how='inner') with pd.ExcelWriter('/Users/xxxx/documents/excels/doctor_data_merge.xlsx', engine='xlsxwriter', options={'strings_to_urls': False}) as writer: df4.to_excel(writer, index=False)if __name__ == '__main__': print_hi('PyCharm')@H_502_8@
总结
以上是内存溢出为你收集整理的python pandas *** 作excel多个sheet合并成一个sheet全部内容,希望文章能够帮你解决python pandas *** 作excel多个sheet合并成一个sheet所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)