することはできマップフロート、転置と合計(column_nameに、column_sum)のペアと辞書作り、各列の各行:あなたの入力サンプルについて
import csv
with open("train.csv", "r") as f:
r = csv.reader(f)
# pull first row i.e A,B,C,D... to use ad the keys
keys = next(r)
# iterate ober the rows mapping to float and transpose
# with zip(*... so rows become columns, one per key
summed_dict = dict(zip(keys, map(sum, zip(*(map(float, row) for row in r)))))
:
を
A,B,C,D,E,f,G,H,I,J,K,L,M,N
63,1,1,145,233,1,2,150,0,2.3,3,0,6,0
67,1,4,160,286,0,2,108,1,1.5,2,3,3,2
67,1,4,120,229,0,2,129,1,2.6,2,2,7,1
出力:
{'K': 7.0, 'D': 425.0, 'B': 3.0, 'G': 6.0, 'f': 1.0, 'N': 3.0, 'M': 16.0, 'C': 9.0, 'A': 197.0, 'E': 748.0, 'L': 5.0, 'H': 387.0, 'I': 2.0, 'J': 6.4}
DictReaderを使用することはできますが、ここではDictReaderを使用することで大きな利点はありません。
import numpy as np
# load array from csv
arr = np.loadtxt('data.csv', dtype=float, delimiter=';', skiprows=0)
n = -1
# get n-th row by fany indexing
row = arr[n,:]
print row
print np.sum(row)
m = 2
# get m-th col by fancy indexing
col = arr[:,m]
print col
print np.sum(col)
それはかなり自明です:
あなたはCSVファイルを意味すると思いますが、正しいですか? – idjaw
はい私はCSVを意味しました – Dandy
ファイルはどのように見えますか? –