1.背景
工作中需要处理多个文件,每个文件里面有重复的数据,剔除重复数据,保留最新的数据
2.代码:
import pandas as pd import os dl= [] #person_list是文件路径 for i in range(person_list_len): #把文件df全部集合进列表dl dl.append(pd.read_csv(person_list[i], encoding='utf-8')) #集合数据 df=pd.concat(dl) #删除重复的数据 df.drop_duplicates(subset=['身份证号'],keep='last',inplace=True) #保存数据到cvs new_file = 'D:\\xxx\\xxx\\合并_%s.csv'%file_name if os.path.exists(new_file): os.remove(new_file) df.to_csv(new_file)