how to transform strings in csv file into float and store them as a list?

Question:

I have csv file that looks like this:

id,type,inc,a,b
1,2,63376.799999999996,0.3061,187
2,1,58087.700000000004,0.3361,178
3,1,52699.9,0.3482,181
4,2,72964.8,0.3186,177
5,4,111589.79999999999,0.268,154
6,4,107618.0,0.2583,150
7,2,87109.2,0.3233,193
8,2,84669.59999999999,0.308,179
9,2,77258.4,0.3247,173

I need to transform values from fields [3], [4] and [5] that are ‘inc’, ‘a’ and ‘b’ into float and than append to households to create a list.

Finally it should print len(households)

I am trying this

import csv 
import scipy.optimize as opt
def read_households(filename):
    households=[]
    fh = open("households.csv", 'r')
    reader = csv.DictReader(fh) #creates object 'reader' to read the file
    for hh in reader:
        reader[3,4,5] = float(reader[3,4,5])
        households.append(hh)
        
        print("lines read:", len(households))
        return(household)

It does not give me anything.

Asked By: Anano Mikadze

||

Answers:

Try this code for get some output from CSV.

import csv 
import scipy.optimize as opt

def read_households(filename):
    households=[]
    fh = open(filename, 'r')
    reader = csv.DictReader(fh)
    for hh in reader:
        hh['inc'] = float(hh['inc'])
        hh['a'] = float(hh['a'])
        hh['b'] = float(hh['b'])
        households.append(hh)
        
    print("lines read:", len(households))
    return households

# call function
read_households('path_of_csv_file')

Result :

lines read: 9
[{'id': '1', 'type': '2', 'inc': 63376.799999999996, 'a': 0.3061, 'b': 187.0},
 {'id': '2', 'type': '1', 'inc': 58087.700000000004, 'a': 0.3361, 'b': 178.0},
 {'id': '3', 'type': '1', 'inc': 52699.9, 'a': 0.3482, 'b': 181.0},
 {'id': '4', 'type': '2', 'inc': 72964.8, 'a': 0.3186, 'b': 177.0},
 {'id': '5', 'type': '4', 'inc': 111589.79999999999, 'a': 0.268, 'b': 154.0},
 {'id': '6', 'type': '4', 'inc': 107618.0, 'a': 0.2583, 'b': 150.0},
 {'id': '7', 'type': '2', 'inc': 87109.2, 'a': 0.3233, 'b': 193.0},
 {'id': '8', 'type': '2', 'inc': 84669.59999999999, 'a': 0.308, 'b': 179.0},
 {'id': '9', 'type': '2', 'inc': 77258.4, 'a': 0.3247, 'b': 173.0}]
Answered By: NIKUNJ KOTHIYA

If you can use pandas then this is a very easy way:

import pandas as pd

df = pd.read_csv('file.csv')
df = df.astype({'inc': 'float', 'a': 'float', 'b': 'float'})
print(df)

print(f"Lines read: {df.shape[0]}")

When run gives:

   id  type       inc       a      b
0   1     2   63376.8  0.3061  187.0
1   2     1   58087.7  0.3361  178.0
2   3     1   52699.9  0.3482  181.0
...
8   9     2   77258.4  0.3247  173.0
Lines read: 9

If you need the output in the list of dictionaries output:

households = df.to_dict('records')

and we are done

Answered By: Fantastic Mr Fox
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.