Re: [問題] 想請問有更有效率的寫法嗎?

作者: forloricever (sigh...)   2014-08-30 07:15:35
※ 引述《sariel0322 (sariel)》之銘言:
: 我想把一個列數相當多的csv檔案
: 把裡面重複的列數給刪除掉
: 我只能想到這種寫法:
: import csv
: rows = []
: a = 0
: o = open("output.csv","w")
: f = open("input.csv","r")
: for row in csv.reader(f):
: rows.append(row[0]+","+row[1]+","+row[2]+","+row[3]+","+row[4]+","+row[5]+","+row[6]+","+row[7]+","+row[8]+","+row[9]+","+row[10])
: for i in set(rows):
: o.write(i+"\n")
: f.close()
: o.close()
: 但由於行數非常多,資料量也大(csv檔案約400mb)
: 因此全部跑完可能需要五天(有寫個計數器來大約計算過,為了節省空間沒列出來)
: 想請問有沒有更有效率的寫法
用 pandas
import pandas as pd
data = pd.read_csv('input.csv')
data.drop_duplicates().to_csv('output.csv')

Links booklink

Contact Us: admin [ a t ] ucptt.com