通过pandas的相关方法来实现数据在excel表中的写入、读取、文件数据更改、删除、表格合并等操作
利用数生成表格并存储文excel文件
import pandas
as pd
import numpy
as np
stu_names
= ["胡歌","林更新","金世佳","丑娟"]
courses
= ['语文', '数学', '英语', 'Python', '体育']
data
= np
.array
([[87., 74., 98., 84., np
.nan
],[79., 69., 61., 99., np
.nan
],[84., 84., 94., 66., np
.nan
],[90., 60., 72., 90., np
.nan
]])
lesson_score
= pd
.DataFrame
(data
,index
= stu_names
, columns
= courses
)
lesson_score
lesson_score
.to_excel
("学生成绩表.xlsx")
ele_names
= ["胡歌","林更新","金世佳","丑娟"]
ele_courses
= ['统计学', '日语']
data
= np
.array
([
[1,np
.nan
],
[np
.nan
, 20],
[20,30],
[666,666]
])
lesson_df
= pd
.DataFrame
(data
, index
= ele_names
,columns
= ele_courses
)
lesson_df
.to_excel
("选修成绩表.xlsx")
lesson_df
读取文件
excel_1
= pd
.read_excel
("学生成绩表.xlsx",index_col
=[0])
excel_1
excel_2
= pd
.read_excel
("选修成绩表.xlsx",index_col
=[0])
excel_2
删除行
excel_1
.drop
("体育",axis
= 1)
excel_1
两个表格的合并
lesson_score
["统计学"] = lesson_df
["统计学"]
lesson_score
["日语"] = lesson_df
["日语"]
lesson_score
算总成绩
lesson_score
["总成绩"] = lesson_score
.sum(axis
= 1)
lesson_score
.to_excel
("总表.xlsx")
lesson_score
转载请注明原文地址:https://tech.qufami.com/read-2774.html