I have a CSV file that looks like this
Item,Price,Calories,Category
Orange,1.99,60,Fruit
Cereal,3.99,110,Box Food
Ice Cream,6.95,200,Dessert
...
and I want to form a Python dictionary in this format:
{'Orange': (1.99, 60, 'Fruit'), 'Cereal': (3.99, 110, 'Box Food'), ... }
I want to make sure the titles of the columns are removed (i.e., the first row is NOT included).
Here is what I've tried so far:
reader = csv.reader(open('storedata.csv'))
for row in reader:
# only needed if empty lines in input
if not row:
continue
key = row[0]
x = float(row[1])
y = int(row[2])
z = row[3]
result[key] = x, y, z
print(result)
However, when I do this, I get a ValueError: could not convert string to float: 'Price', and I don't know how to fix it. I want to keep these three values in a tuple.
Thanks!
reader. If this is adataframecan you use the.info()method on the reader.<class 'pandas.core.frame.DataFrame'> RangeIndex: 4 entries, 0 to 3 Data columns (total 4 columns): 0 4 non-null object 1 4 non-null object 2 4 non-null object 3 4 non-null object dtypes: object(4) memory usage: 256.0+ bytes