我滴个妈也,原来pandas有中文官网https://www.pypandas.cn/
根据pandas的用户指南,https://pandas.pydata.org/docs/user_guide/io.html,我们发现pandas可不仅仅是操作excel这么简单
![](http://upload-images.jianshu.io/upload_images/16670204-2f0d35e31302981f.png)
这里我们着看其针对excel的API,包括读取excel所用的read_excel
和写excel所用的to_excel
一. read_excel //读取excel
pandas.read_excel(io, sheet_name=0, header=0, names=None, index_col=None, usecols=None,
squeeze=False, dtype=None, engine=None, converters=None, true_values=None,
false_values=None, skiprows=None, nrows=None, na_values=None, keep_default_na=True,
na_filter=True, verbose=False, parse_dates=False, date_parser=None, thousands=None,
comment=None, skipfooter=0, convert_float=True, mangle_dupe_cols=True, storage_options=None)
重点参数:
参数 | 说明 |
---|---|
io | 输入输出流,可以是:路径str, URL,bytes, ExcelFile, xlrd.Book, path object, or file-like object |
sheet_name | 表名,可以是:str, int, list, or None, 默认索引0 , 如果是列表, 则返回以列表为key的字典, 如果传入None则读所有表 |
header | 列索引(表头)。可以是int, list of int, default 0 |
index_col | 行索引。int, list of int, default None |
usecols | 使用的列。可以是:int,str, list of int, default None |
skiprows | 跳过的行 |
names | 要使用的列名列表,(用列表给表头赋值) |
dtype | 设置列的类型 |
parse_dates | 指定解析成日期格式的列 |
date_parser | 解析日期格式的函数,传入 function |
na_values | 识别为缺失值的元素 |
converters | 值转换器 传入dict,默认None |
true_values | 视为真 传入list,默认None |
false_values | 视为假 传入list,默认None |
1. io参数 //输入输出对象
最简单用法
df1 = pd.read_excel("goods_base.xls")
可以传入ExcelFile对象
excel1 = pd.ExcelFile("goods_base.xls")
df1 = pd.read_excel(excel1)
2. sheet_name参数 //表名
表名,可以是:str, int, list, or None, 默认索引0 , 如果是列表, 则返回以列表为key的字典, 如果传入None则读所有表
(1). 默认读取索引0
![](http://upload-images.jianshu.io/upload_images/16670204-b9bece2cf13ef486.png)
(2). 可以添加索引或表名