题目:
使用 pandas 筛选表格中的重复数据,将筛选后的表格保存到新的 excel 文件中。
视频教程:
Python入门题031:excel表格筛选重复数据
代码:
import pandasscores = pandas.read\_excel('./storage/成绩单.xlsx')print('------ 当前表格:')print(scores)print('------ 开始筛选重复数据:')# 新建个 DataFrame 用来保存过滤后的数据new\_scores = pandas.DataFrame()# 用来标记是否已存在existed\_name = {}for index, row in scores.iterrows(): if row\['姓名'\] in existed\_name: print('发现重复项:', row\['姓名'\], row\['成绩'\]) continue existed\_name\[row\['姓名'\]\] = True new\_scores = new\_scores.append(row, ignore\_index=True)print('------ 筛选后的表格:')print(new\_scores)print('------ 正在保存到新表格中')new\_scores.to\_excel('./storage/成绩单-02.xlsx', index=False)print('------ 完成!')效果:
32804260