김예린 - 3 months ago 47

Python Question

I have csv file with [NEAR_DIST] (<-row 7). I would like to know how can I get the mean and STD of this array.

I used csv and numpy:

`# -*- coding: utf-8 -*-`

import csv

import numpy

with open('C:\\test.csv') as csvfile:

readCSV = csv.reader(csvfile, delimiter=',')

distance = []

distance.append(float(x) for x in csvfile)

for row in readCSV:

distance = row[7]

a = numpy.array(distance, dtype = 'float_')

b = numpy.sum(a)

print(b)

Answer

The question is a little unclear. My assumption is that you have some data of the form:

01,02,03,04,05,06,07,08,09,..

11,12,13,14,15,16,17,18,19,..

21,22,23,24,25,26,27,28,29,..

and you want to find the sum of elements in the 7th column. i.e. 7 + 17 + 27 + ... in this example.

This should work:

```
import numpy
data = numpy.genfromtxt("a.csv", delimiter=",")
data[:, 6].sum() # 6 because indices start from 0 in python
```