在進(jìn)行日常的數(shù)據(jù)分析統(tǒng)計(jì)時(shí),很多時(shí)候我們需要將不同數(shù)據(jù)寫入到同一個(gè)Excel文件的不同工作簿或同一個(gè)工作簿中。本文主要介紹 Python 如何將多個(gè) DataFrame 寫入同一個(gè) Excel 不同或同一個(gè)工作簿中。
將多個(gè) DataFrame 寫入到 Excel 的不同工作簿中
實(shí)現(xiàn)代碼如下:
# -*- coding: utf-8 -*-import pandas as pdimport inspect class ExcelWriter: def __init__(self, filepath, student_data, performance_data): self.writer = pd.ExcelWriter(filepath) self.student_data = student_data self.performance_data = performance_data @staticmethod def save_execl(writer, sheet_name, header, data, startcol=0, startrow=0): ''' :param writer: pd.ExcelWriter(filepath) 實(shí)例 :param sheet_name: sheet頁(yè) :param header: 字段表頭 :param data: 數(shù)據(jù) list :param startcol: 起始列 :param startrow: 起始行 :return: ''' header = header.split(',') df = pd.DataFrame(data) df.to_excel(writer, sheet_name=sheet_name, header=header, index=False, startcol=startcol, startrow=startrow) writer.save() def student_information(self): ''' 將考生信息寫入'考生信息表' :param student_data: :return: ''' header = '學(xué)號(hào),姓名,性別,年齡' ExcelWriter.save_execl(self.writer,'考生信息表', header, self.student_data) def performance_information(self): ''' 將考生成績(jī)寫入'成績(jī)信息表' :param performance_data: :return: ''' header = '學(xué)號(hào),語(yǔ)文,數(shù)學(xué),體育' ExcelWriter.save_execl(self.writer,'成績(jī)信息表', header, self.performance_data) def run_all(self): for function in inspect.getmembers(self, predicate=inspect.ismethod): if function[0] not in ['__init__','run_all'] : function[1]() if __name__ == '__main__': student_data = [['9527', '張三', '男', 27]] performance_data = [['9527', 77, 88, 99]] excelWriter = ExcelWriter('info.xlsx', student_data, performance_data) excelWriter.run_all()
輸出 info.xlsx 如下:
Python如何將多個(gè) DataFrame 寫入到 Excel 的相同工作簿中
實(shí)現(xiàn)代碼如下:
# -*- coding: utf-8 -*-import pandas as pdimport inspectclass ExcelWriter: def __init__(self, filepath, student_data, performance_data): self.writer = pd.ExcelWriter(filepath) self.student_data = student_data self.performance_data = performance_data @staticmethod def save_execl(writer, sheet_name, header, data, startcol=0, startrow=0): ''' :param writer: pd.ExcelWriter(filepath) 實(shí)例 :param sheet_name: sheet頁(yè) :param header: 字段表頭 :param data: 數(shù)據(jù) list :param startcol: 起始列 :param startrow: 起始行 :return: ''' header = header.split(',') df = pd.DataFrame(data) df.to_excel(writer, sheet_name=sheet_name, header=header, index=False, startcol=startcol, startrow=startrow) writer.save() def student_with_performance_information(self): ''' 考生成績(jī)信息表 :param student_data: :param performance_data: :return: ''' header = '學(xué)號(hào),姓名,性別,年齡' ExcelWriter.save_execl(self.writer, '考生成績(jī)信息表', header, self.student_data) header = '學(xué)號(hào),語(yǔ)文,數(shù)學(xué),體育' ExcelWriter.save_execl(self.writer,'考生成績(jī)信息表', header, self.performance_data, startrow=len(self.student_data) + 2) def run_all(self): for function in inspect.getmembers(self, predicate=inspect.ismethod): if function[0] not in ['__init__','run_all'] : function[1]()if __name__ == '__main__': student_data = [['9527', '張三', '男', 27]] performance_data = [['9527', 77, 88, 99]] excelWriter = ExcelWriter('info.xlsx', student_data, performance_data) excelWriter.run_all()
輸出 info.xlsx 如下: