Python-pandas处理数据-删除重复数据

Python-pandas处理数据-删除重复数据

  • 摘要
    • 应用场景
    • 代码实现
    • 结果

摘要

本文介绍实际情况中如何删除一行完全一样的数据

应用场景

如图,在实际情况中,有这样一组数据:数据中有很多行的数据都是相同的,为了删除这些多余的数据,可以利用Python的pandas库来清洗数据。
Python-pandas处理数据-删除重复数据_第1张图片

代码实现

import pandas as pd
data = pd.read_csv('D:\\CMT\\casm-fit\\0303\\temp\\temp2.csv') #read data
data.ix[:5] #read first 5 lines data
newdata = data.drop_duplicates(subset=['comp', 'energy'], keep='first')
 # output to a file
df=newdata.to_csv('D:\\CMT\\casm-fit\\0303\\temp\\ce_pr_nodupli.csv', sep=',', header=True, index=True)

结果

Python-pandas处理数据-删除重复数据_第2张图片

你可能感兴趣的:(数据处理)