Reputation: 71
S I have a csv file with a bunch of data in it. when I try a few methods to convert the whole thing to a float it gives me an error saying I can not change the contents of the items in the first column to a string (it really just breaks after it hits the first column so my assumption is the contents of the first row are not able to be converted and that is fine).
I would like to be able to skip the first column and convert the rest of my contents to a float.
When I read in my csv file:
with open ('timecards.csv') as tcards:
tcards = csv.reader(tcards)
tfloat =[]
for i in tcards:
print(i)
I get the output of:
['51-4678119', '7.6', '3.1', '1.4', '4.1', '6.4', '7.7', '6.6']
['87-6664864', '5.2', '3.8', '7.5', '7.8', '7.0', '2.1', '3.6', '7.1', '5.1']
['07-3318962', '2.5', '6.9', '7.9', '7.7', '1.1', '2.6', '5.9', '6.1', '7.5', '3.6']
['06-4413296', '5.9', '4.2', '6.2', '3.1', '2.2', '7.6', '4.7', '6.7', '1.9', '7.4']
['58-5924013', '6.1', '6.2', '1.4', '3.2', '1.4', '1.2', '2.4', '4.4', '5.8', '2.2']
['45-9277595', '6.8', '1.6', '5.1', '3.2', '2.6', '7.1', '4.9', '1.6', '7.2', '2.8']
['04-0002399', '1.1', '6.2', '2.4', '3.8', '2.7', '7.0', '6.2', '2.3', '6.2', '4.3']
['27-7347997', '3.1', '2.1', '4.2', '3.7', '6.5', '7.0', '7.0', '3.9', '3.5', '4.6']
['38-0238479', '7.1', '2.1', '2.7', '6.8', '3.9', '2.6', '2.0', '1.5', '2.6', '1.3']
and so on. I would like to allow my first column to stay a string I guess and convert the rest of my contents to a float. Thanks!
Upvotes: 0
Views: 278
Reputation: 736
import csv
with open ('/tmp/timecards.csv') as tcards:
for i in csv.reader(tcards):
print([i[0]] + list(map(float, i[1:])))
result:
['51-4678119', 7.6, 3.1, 1.4, 4.1, 6.4, 7.7, 6.6]
['87-6664864', 5.2, 3.8, 7.5, 7.8, 7.0, 2.1, 3.6, 7.1, 5.1]
['07-3318962', 2.5, 6.9, 7.9, 7.7, 1.1, 2.6, 5.9, 6.1, 7.5, 3.6]
['06-4413296', 5.9, 4.2, 6.2, 3.1, 2.2, 7.6, 4.7, 6.7, 1.9, 7.4]
['58-5924013', 6.1, 6.2, 1.4, 3.2, 1.4, 1.2, 2.4, 4.4, 5.8, 2.2]
['45-9277595', 6.8, 1.6, 5.1, 3.2, 2.6, 7.1, 4.9, 1.6, 7.2, 2.8]
['04-0002399', 1.1, 6.2, 2.4, 3.8, 2.7, 7.0, 6.2, 2.3, 6.2, 4.3]
['27-7347997', 3.1, 2.1, 4.2, 3.7, 6.5, 7.0, 7.0, 3.9, 3.5, 4.6]
['38-0238479', 7.1, 2.1, 2.7, 6.8, 3.9, 2.6, 2.0, 1.5, 2.6, 1.3]
Upvotes: 1
Reputation: 82765
This is one approach using map
Ex:
result =[]
with open ('timecards.csv') as tcards:
tcards = csv.reader(tcards)
for row in tcards:
c1, *lst = row # Column1 and rest of columns
result.append([c1] + list(map(float, lst)))
Upvotes: 2
Reputation: 4449
>>> tcards = [['51-4678119', '7.6', '3.1', '1.4', '4.1', '6.4', '7.7', '6.6'],
['87-6664864', '5.2', '3.8', '7.5', '7.8', '7.0', '2.1', '3.6', '7.1', '5.1']]
>>> [[row[0]] + list(map(float, row[1:])) for row in tcards]
[['51-4678119', 7.6, 3.1, 1.4, 4.1, 6.4, 7.7, 6.6],
['87-6664864', 5.2, 3.8, 7.5, 7.8, 7.0, 2.1, 3.6, 7.1, 5.1]]
>>> # alternative (maybe simpler?)
>>> [[row[0]] + [float(val) for val in row[1:]] for row in tcards]
[['51-4678119', 7.6, 3.1, 1.4, 4.1, 6.4, 7.7, 6.6],
['87-6664864', 5.2, 3.8, 7.5, 7.8, 7.0, 2.1, 3.6, 7.1, 5.1]]
Upvotes: 0