基本介紹
網(wǎng)站建設(shè)哪家好,找成都創(chuàng)新互聯(lián)公司!專注于網(wǎng)頁(yè)設(shè)計(jì)、網(wǎng)站建設(shè)、微信開(kāi)發(fā)、重慶小程序開(kāi)發(fā)、集團(tuán)企業(yè)網(wǎng)站建設(shè)等服務(wù)項(xiàng)目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了馬尾免費(fèi)建站歡迎大家使用!pandas是Python數(shù)據(jù)挖掘、數(shù)據(jù)分析中常用的庫(kù)。而DataFrame生成excel中的sheet表,以及在excel中增加sheet表,在數(shù)據(jù)分
析中也經(jīng)常用到。這里以一個(gè)簡(jiǎn)單例子介紹這兩個(gè)功能的用法,以備不時(shí)之需。
例子:
#!/usr/bin/env python # -*- coding: utf-8 -*- """ Version: 0.1 Author: Wang Pei License: Copyright(c) 2017 Pei.Wang Summary: (1)padas dataframe生成excel (2)excel中增加sheet表 """ import sys import pandas as pd from openpyxl import load_workbook reload(sys) sys.setdefaultencoding('utf-8') """ padas dataframe生成excel """ def dataFrame2sheet(dataframe,excelWriter): # DataFrame轉(zhuǎn)換成excel中的sheet表 dataframe.to_excel(excel_writer=excelWriter, sheet_name="info1",index=None) dataframe.to_excel(excel_writer=excelWriter, sheet_name="info2",index=None) dataframe.to_excel(excel_writer=excelWriter, sheet_name="info3",index=None) excelWriter.save() excelWriter.close() """ excel中新增sheet表 """ def excelAddSheet(dataframe,excelWriter): book = load_workbook(excelWriter.path) excelWriter.book = book dataframe.to_excel(excel_writer=excelWriter,sheet_name="info5",index=None) excelWriter.close() if __name__ == '__main__': # 數(shù)據(jù)集 dataSet = [ {"姓名": "張三", "年齡": 23, "性別": "男"}, {"姓名": "李四", "年齡": 25, "性別": "男"}, {"姓名": "王五", "年齡": 21, "性別": "女"} ] # excelPath excelPath="G:\cmcm\ReportAuto\\02TestData\\x.xlsx" # 生成DataFrame dataframe = pd.DataFrame(dataSet) #創(chuàng)建ExcelWriter 對(duì)象 excelWriter=pd.ExcelWriter(excelPath,engine='openpyxl') # #生成excel #dataFrame2sheet(dataframe,excelWriter) #excel中增加sheet excelAddSheet(dataframe,excelWriter)