python - Performing calculations on a tab-delimited file -
i'm still learning python , still don't know how work arrays.
i'm want read tab-delimited file example (but in case have 400 rows):
col1 col2 0.0001 0.6 0.0001 0.5 0.000006 0.8 0.0001 0.0003 0.002 1 0.002 3
i want following output:
col1 col2 0.0001 0.36676667 0.000006 0.8 0.002 2
so wanna keep same value in col1 taking mean of values in col2 corresponds same value in col1.
i can read array using :
arr = np.genfromtxt('test.csv', dtype=none, delimiter='\t', skiprows=1)
but don't know how make these operations , making new file new generated data.
thanks lot help!
use collections.defaultdict
list
default argument.
take value first column key, , append second value.
import csv collections import defaultdict # gather data csv file d = defaultdict(list) open('data.csv', 'r') csvfile: reader = csv.reader(csvfile, delimiter='\t') row in reader: d[float(row[0])].append(float(row[1])) # print mean. k in d.keys(): print k, sum(d[k])/len(d[k])
Comments
Post a Comment