基础
read_excel()
#定义:
pandas.read_excel(io, # 文件路径
sheet_name=0, # 索引表的位置,可用位置和名字进行索引
header=0,
skiprows=None,
skip_footer=0,
index_col=None,
names=None,
usecols=None,
parse_dates=False,
date_parser=None,
na_values=None,
thousands=None,
convert_float=True,
converters=None,
dtype=None,
true_values=None,
false_values=None,
engine=None,
squeeze=False,
**kwds)
#实例:
import pandas as pd
excel_path = './workbook.xlsx'
df = pd.read_excel(excel_path,sheet_name='Sheet1')
# 数据的大小
height,width = df.shape
# 获取第一行/列名,tolist()转换成list
index = df.columns.values.tolist()
# 获取指定列
data = df.ix[:,2:]
# 根据列名获取数据
data = df.loc[:,'列名']
# 合并数据
new_data = pd.merge(df2,df1)
ExcelFile.parse()
定义:
def parse(
self,
sheet_name=0,
header=0,
names=None,
index_col=None,
usecols=None,
squeeze=False,
converters=None,
true_values=None,
false_values=None,
skiprows=None,
nrows=None,
na_values=None,
parse_dates=False,
date_parser=None,
thousands=None,
comment=None,
skipfooter=0,
convert_float=True,
mangle_dupe_cols=True,
**kwds,
):
实例:
xls = pd.ExcelFile('data_nb0.xlsx')
df = xls.parse('sheet1')
to_excel()
定义:
DataFrame.to_excel(excel_writer, # 文件路径或者现有的ExcelWriter
sheet_name='Sheet1', # DataFrame表 名称
na_rep='', # 缺失数据
float_format=None,
columns=None,
header=True, # 列名
index=True, # 行名
index_label=None,
startrow=0,
startcol=0,
engine=None,
merge_cells=True,
encoding=None,
inf_rep='inf',
verbose=True,
freeze_panes=None)
实例:
data_list=[[1, 2, 3, 4], [5, 6, 7, 8]]
df = pd.DataFrame(data_list)
with ExcelWriter('path_to_file.xlsx') as writer:
df.to_excel(writer)
进阶
使用Pandas读取大型Excel文件
官方api
网友评论