基本介绍
pandas是python数据挖掘、数据分析中常用的库。而dataframe生成excel中的sheet表,以及在excel中增加sheet表,在数据分
析中也经常用到。这里以一个简单例子介绍这两个功能的用法,以备不时之需。
例子:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
#!/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转换成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__' : # 数据集 dataset = [ { "姓名" : "张三" , "年龄" : 23 , "性别" : "男" }, { "姓名" : "李四" , "年龄" : 25 , "性别" : "男" }, { "姓名" : "王五" , "年龄" : 21 , "性别" : "女" } ] # excelpath excelpath = "g:\cmcm\reportauto\\02testdata\\x.xlsx" # 生成dataframe dataframe = pd.dataframe(dataset) #创建excelwriter 对象 excelwriter = pd.excelwriter(excelpath,engine = 'openpyxl' ) # #生成excel #dataframe2sheet(dataframe,excelwriter) #excel中增加sheet exceladdsheet(dataframe,excelwriter) |
以上这篇pandas 生成excel 增加sheet表的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/wangpei1949/article/details/72794846