天天看點

Python-pandas處理資料-删除重複資料摘要

Python-pandas處理資料-删除重複資料

  • 摘要
    • 應用場景
    • 代碼實作
    • 結果

摘要

本文介紹實際情況中如何删除一行完全一樣的資料

應用場景

如圖,在實際情況中,有這樣一組資料:資料中有很多行的資料都是相同的,為了删除這些多餘的資料,可以利用Python的pandas庫來清洗資料。

Python-pandas處理資料-删除重複資料摘要

代碼實作

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處理資料-删除重複資料摘要