我正在嘗試計算CSV文件列中的重複值並將值返回給python中的另一個CSV列。在CSV文件的特定列中計數重複值並將該值返回到另一列(python2)
例如,我的CSV文件:
KeyID GeneralID
145258 KL456
145259 BG486
145260 HJ789
145261 KL456
我想實現的是計算有多少數據具有相同的GeneralID
並將其插入新的CSV列。例如,
KeyID Total_GeneralID
145258 2
145259 1
145260 1
145261 2
我試圖使用拆分方法拆分每列,但它不能很好地工作。
我的代碼:
case_id_list_data = []
with open(file_path_1, "rU") as g:
for line in g:
case_id_list_data.append(line.split('\t'))
#print case_id_list_data[0][0] #the result is dissatisfying
#I'm stuck here..
你用什麼python版本導入集合庫?我正在使用python v 2.6.6,並且出現錯誤 'from collections import Counter' 'ImportError:無法導入名稱計數器' – yunaranyancat
計數器爲2.7+,但您可以在此獲取源代碼:http:// code .activestate.com/recipes/576611-counter-class/ –