I am finding the most optimized way to create an integer array with the help of data from a csv file.
The csv file("sample.csv") data is like,
| prediciton1 | prediction2 | prediction3 |
|---|---|---|
| low | low | low |
| low | high | high |
where low = 1, high = 3,
i want to read these data from the csv and make an array that looks like,
array =
[[1,1,1],
[1,3,3]]
import csv
import sys
num1 = 'low'
num2 = 'high'
csv_file = csv.reader(open('sample.csv', "r"), delimiter=",")
count = 0
for row in csv_file:
if count == 1:
if num1 == row[0]:
dat1 = 1
elif num2 == row[0]:
dat1 = 3
if num1 == row[1]:
dat2 = 1
elif num2 == row[1]:
dat2 = 3
if num1 == row[2]:
dat3 = 1
elif num2 == row[2]:
dat3 = 3
count = count + 1
array =[dat1,dat2,dat3]
This approach works but seems much inefficient. Finding an alternative and optimized way to achieve this.