首页 > 解决方案 > 使用基于标头的python从文件中删除重复内容

问题描述

我是 python 新手,我必须根据某些条件删除重复数据,即如果 symb 变量与相同的 symb 变量匹配,然后删除重复的 symb 变量并关联数据,例如(dtype、iotype、bias、slpe、unit、
max、min , client_no , 路径 , 结束) 。我在这里附上一个文件。

填写下面的链接

从这里下载文件。

标签: pythonfile

解决方案


您可以使用 pandas.DataFrame.drop_duplicates 方法从文件中删除重复项

import pandas as pd     # Import Pandas DF
input = "sample.txt"    # Input File
output = sample_original.txt    # output file
df = pd.read_csv(input, sep="\t or ,")    # Copy the files into the Dataframe \t is used for tab seperation , is used for comma seperation depends on how the text is spaced 
df.drop_duplicates(subset=None, inplace=True)  # Drop the duplicates Using inplace = True modifies original file also if you do not want to do this drop the inplace option
df.to_csv(file_name_output, index=False) # Export the content without duplicates into a txt file.

我假设您的文件是 txt,因为我能够将其导入记事本

这是熊猫文档的链接

pandas.DataFrame.drop_duplicates


推荐阅读