读写excel需要用到:
pip install xlrd -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install xlwt -i https://pypi.tuna.tsinghua.edu.cn/simple
示例代码:
import numpy as np
import pandas as pd
df1 = pd.DataFrame(data = np.random.randint(0,50,size = [50,5]), # 薪资情况
columns=['IT','化⼯','⽣物','教师','⼠兵'])
df2 = pd.DataFrame(data = np.random.randint(0,50,size = [150,3]),# 计算机科⽬的考试成绩
columns=['Python','Tensorflow','Keras'])
# 保存到当前路径下,⽂件命名是:salary.xls
df1.to_excel('./salary.xls',
sheet_name = 'salary',# Excel中⼯作表的名字
header = True,# 是否保存列索引
index = False) # 是否保存⾏索引,保存⾏索引
pd.read_excel('./salary.xls',
sheet_name=0,# 读取哪⼀个Excel中⼯作表,默认第⼀个
header = 0,# 使⽤第⼀⾏数据作为列索引
names = list('ABCDE'),# 替换⾏索引
index_col=1)# 指定⾏索引,B作为⾏索引
# ⼀个Excel⽂件中保存多个⼯作表
with pd.ExcelWriter('./data.xlsx') as writer:
df1.to_excel(writer,sheet_name='salary',index = False)
df2.to_excel(writer,sheet_name='score',index = False)
pd.read_excel('./data.xlsx',
sheet_name='salary') # 读取Excel中指定名字的⼯作表