python - 使用基于标头的python从文件中删除重复内容
问题描述
我是 python 新手,我必须根据某些条件删除重复数据,即如果 symb 变量与相同的 symb 变量匹配,然后删除重复的 symb 变量并关联数据,例如(dtype、iotype、bias、slpe、unit、
max、min , client_no , 路径 , 结束) 。我在这里附上一个文件。
填写下面的链接
解决方案
您可以使用 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,因为我能够将其导入记事本
这是熊猫文档的链接
推荐阅读
- swift - 将 for 循环替换为映射方法
- java - 从通配符列表中删除每个第 N 个元素 - Java
- python - 你如何创建一个自定义的 Kivymd MDDialog,它将弹出 GridLayout 或任何其他默认设置的小部件
- deep-learning - PyTorch - 模型参数权重的意外形状
- python - 如何更新嵌套字典中的值
- c++ - 评估变量的构造函数等价物
- ms-word - 使用 gtsummary 和 flextable 在 rmarkdown 中生成表
- websocket - Mosquitto Broker 服务拒绝在 websockets 上启动
- mysql - 在多个条件下连接四个表
- c - 如何使用 C 程序永久存储信息?