Reputation: 1
I have a CSV file and need to remove or just skip or replace with 0
some missing data that is represented as None
in the list. Row1 = [1, 2, 3, 4, none, 5, 6, 7, 8, 9]
and row2 = [1, 2, 3, 4, 5, 6, none, 7, 8, 9]
in the CSV file.
counter=0
for lines in csv:
list_all=lines.split(',')
list_all=lines.strip()
counter+=1
for ch in range(len(list_all)):
if list_all[ch]=='?':
list_all.replace('?',0)
list_0=float(list_all[0])
list0.append(list_0)
avelist0=(sum(list0)/counter)
I need to get the sum of all the values from each column; not including the None
, of course. I keep getting an error:
TypeError: Can't convert 'int' object to str implicitly.
I'm still new to Python and I would like to do this with a for
loop if possible. I can't use list comprehensions.
So the average of first list would be 22.5
and 22.5
for the second list.
Upvotes: 0
Views: 65
Reputation: 59671
Because you can't use list comprehensions, what you want to do is map
over the list:
l = [1,2,3,4,None,5,6,7,8,9]
new_l = map(lambda x: 0 if x.lower().strip() == 'none' else int(x), l)
print new_l # [1, 2, 3, 4, 0, 5, 6, 7, 8, 9]
Then if you want sum the entire list, use sum
:
sum(new_l) # returns 45
Upvotes: 1