2017-08-05 70 views
0

我有一個簡單的2列的CSV和需要找到每個鍵 即 輸入CSV平均尋找者皆平均值從csv在python

A,2 
B,3 
A,1 
C,2 
B,2 
D,4 
C,2 

所需的輸出

{'A': 1.5, 'B': 2.5, 'C': 2, 'D': 4} 

到目前爲止的代碼:

pythoncsvfile = open("data.csv") 
csv_reader = csv.reader(csvfile, delimiter=',') 
for row in csv_reader: 
    print (row[0],row[1]) 
+4

這是一個很好的,明確的問題陳述。現在,嘗試編寫一些代碼來實現它。如果您遇到困難,請告訴我們您卡在哪裏以及爲什麼。 –

+1

你有'熊貓'嗎? –

+0

您是否考慮過適合的容器數據類型? – wwii

回答

2

選項A

使用csv

import csv 
import collections 

out = collections.defaultdict(list) 
with open('file.csv') as f: 
    for line in csv.reader(f): 
     out[line[0]].append(int(line[1])) 

for k in out: 
    out[k] = sum(out[k])/len(out[k]) 

print(dict(out)) 

{'A': 1.5, 'B': 2.5, 'C': 2.0, 'D': 4.0} 

選項B

使用pandas

import pandas as pd 

df = pd.read_csv('file.csv', header=None, names=['Key', 'Value']) 
out = df.groupby('Key').mean() 

print(out.Value.to_dict()) 

{'A': 1.5, 'B': 2.5, 'C': 2.0, 'D': 4.0} 
+0

不錯的解決方案,但我想知道你爲什麼設置'as_index = False'。如果你不這樣做,你可以說'out.Value.to_dict()'來獲得請求的OP格式。 –

+0

@JohnZwinck'Key'列成爲索引。我不喜歡這看起來如何。真的,就是這樣。 :p –

+0

@JohnZwinck但是,這是一個不錯的主意:)謝謝。 –

1

我想你可以使用下面的代碼:

import csv 
from collections import OrderedDict 

data = OrderedDict() 

with open('data.csv', 'rb') as csvfile: 
    content = csv.reader(csvfile, delimiter=',') 
    for index, value in content: 
     if (not data.has_key(index)): 
      #initialize 
      data[index] = {'times':1, 'total':float(value)} 
     else: 
      #index already present 
      data[index] = {'times': data[index]["times"]+1, 'total':data[index]["total"]+float(value)} 

def average(data): 
    results = OrderedDict() 

    for index, values in data.iteritems(): 
     results[index] = values["total"]/values["times"] 

    return results 

print average(data) 

實例與數據結果:

OrderedDict([('A', 1.5), ('B', 2.5), ('C', 2.0), ('D', 4.0)]) 

HTH