Apologies for asking a question that have been asked a hundred times before, I'm new to Python and none of the solutions I've found seems to solve my problem.
I have a nested list from a csv file called diabetes, I read in the file and comma separate the elements like this
for line in open("diabetes.csv"):
lst=line.strip().split(",")
print(lst)
which prints out the following
['10', '101', '86', '37', '0', '45.6', '1.136', '38', '1']
['2', '108', '62', '32', '56', '25.2', '0.128', '21', '0']
['3', '122', '78', '0', '0', '23', '0.254', '40', '0']
Now my problem is
- I need to make a separate list containing only the third element of each list (lst[2])
- I need to convert it into floats instead of strings.
I'm using Python 3.6 and I'm pulling my hair out here.
pandasor the standard librarycsvmodule. Try to avoid reinventing-the-wheel.col3 = []andcol3.append(float(lst[2]))to it in every loop. Although I agree using the standardcsvmodule may be better, a custom implementation does provide more flexibility.