Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read numbers in python from csv file?

I have a csv file and I have to compute the mean for some of the columns. That's how I did:

file=csv.reader(open('tab.csv','r'))
n=[]
for row in file:
    n.append(row[8])

So I have a list of string : n=['','','1.58'...] How can I convert these to float? I tried with :

n_values=np.array(n)
n_values[n=='']='0'
values=n_values.astype(np.float)
np.mean(values)

But the mean is not correct because I should skip the empty strings not counting. Thank for your help!

like image 360
Alice Avatar asked Jul 21 '15 10:07

Alice


People also ask

How do I read a CSV file in python with Numbers?

Reading numbers in a CSV file with quotes: QUOTE_ALL then . writerow() will quote all fields and the numbers will now be stored in quotes. To read the numbers from each row, we make use of the reader object from CSV library and store all the rows within a list 'output', which we would also print afterward.

When you read CSV file using csv Reader () function it returns the values in object?

Example 1: Read CSV files with csv. reader() is used to read the file, which returns an iterable reader object. The reader object is then iterated using a for loop to print the contents of each row.


1 Answers

Just cast as you append:

 n.append(float(row[8]))

If there are empty strings catch those before appending.

try:
    n.append(float(row[8]))
except ValueError:
   continue

Or you might want to try pandas, in particular pandas.read_csv:

import pandas as pd

df = pd.read_csv("in.csv")
print(df["col_name"].mean())
like image 194
Padraic Cunningham Avatar answered Sep 19 '22 21:09

Padraic Cunningham