Hi, i am doing my second guided project. I am doing two projects. This is my first one i am working on and its an independent project to help me learn.
Basically I am just trying to calculate the avg of all the rows. The problem is that the data in the csv file is actually a string. ( There are over 1000 rows). Below is how the data looks in excel.
191.9p
196.9p
196.9p
Here is my code:
#Create an empty varaible and poulate that by using a for loop
diesel_price = 0
for row in fuel_data [1:]:
avg_diesel = float(row[2])
diesel_price +=1
avg_diesel_sum = diesel_price + avg_diesel
avg_diesel_price = diesel_price / len(fuel_data[1:])
Here is the error:
~\AppData\Local\Temp/ipykernel_9000/722415982.py in
3
4 for row in fuel_data [1:]:
----> 5 avg_diesel = float(row[2])
6 diesel_price +=1
7 avg_diesel_sum = diesel_price + avg_diesel
ValueError: could not convert string to float: ‘191.9p’
I am just looking for guidance here please. Is there a way to convert this in python? or is it better to actual convert these values in excel?
Click on triangle bullet to view a quick example below:
example
x = '91.9p'
y = x.replace('p', "") # replace the 'p' with a nothing " " using the quotation marks
z = float(y) # now that the p has been removed you can convert to float
x
str (<class 'str'>)
'91.9p'
y
str (<class 'str'>)
'91.9'
z
float (<class 'float'>)
91.9
opened_file = open('ShellPrices.csv')
from csv import reader
read_file = reader(opened_file)
# Below we create a list we can use
fuel_data = list(read_file)
#Create an empty varaible and poulate that by using a for loop
diesel_price = 0
for row in fuel_data [1:]:
avg_diesel = float (row[2])
diesel_price_sum = diesel_price + avg_diesel
avg_diesel_price = diesel_price_sum / len(fuel_data[1:])
Hi am really sorry, it must be the dataset, i copied it from the web. When i trimmed it down it worked. so its not the code. There must be a corrupted cell ( maybe it was empty cell) I’ve been through it all.