题目:
使用 pandas 筛选表格中的重复数据,将筛选后的表格保存到新的 excel 文件中。
视频教程:
Python入门题031:excel表格筛选重复数据
代码:
import pandas
scores = 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('------ 完成!')
效果:
页面更新:2024-05-20
本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828
© CopyRight 2020-2024 All Rights Reserved. Powered By 71396.com 闽ICP备11008920号-4
闽公网安备35020302034903号