1

I am manipulating .csv files. I have to loop through each column of numeric data in the file and enter them into different lists. The code I have is the following:

import csv

salto_linea = "\n"
csv_file = "02_CSV_data1.csv"

with open(csv_file, 'r') as csv_doc:    
    doc_reader = csv.reader(csv_doc, delimiter = ",")   
    mpg = []
    cylinders = []
    displacement = []
    horsepower = []
    weight = []
    acceleration = []
    year = []
    origin = []
    lt = [mpg, cylinders, displacement, horsepower, 
          weight, acceleration, year, origin]
    
    for i,ln in zip(range (0,9),lt):
        print(f"{i} -> {ln}")
        for row in doc_reader:
            y = row[i]
            ln.append(y)

In the loop, try to have range() serve me as an index so that in the nested for loop, it loops through the first column (the first element of each row in the csv) and feeds it into the first list of 'lt'. The problem I have is that I go through the data column and enter it, but range() continues to advance in the first loop, ending the nesting, thinking that it would iterate i = 1, and that new value of 'i' would enter again. the nested loop traversing the next column and vice versa. I also tried it with some other while loop to iterate a counter that adds to each iteration and serves as an index but it didn't work either. How I can fill the sublists in 'lt' with the data which is inside the csv file??

6
  • Sorry, I didn't get what your question is. (You actually haven't asked one) Commented Aug 14, 2022 at 11:57
  • The csv reader will be consumed on the first iteration. Put the loops round the other way: for row in doc_reader: for i, ln in enumerate(lt): ln.append(row[i]). Commented Aug 14, 2022 at 12:19
  • what is the specific question that you are asking please ? Commented Aug 14, 2022 at 13:28
  • Oh, I'm sorry @D.L this is the question: How I can fill the sublists in 'lt' with the data which is inside the csv file?? Commented Aug 14, 2022 at 14:39
  • @molik I gave you the answer. Why don't you try it? Commented Aug 14, 2022 at 15:14

1 Answer 1

0

without seing the ontents of the CSV file itself, the best way of reading the data into a table is with the pandas module, which can be done in one line of code.

import pandas as pd


df = pd.read_csv('02_CSV_data1.csv')

this would have read all the data into a dataframe and you can work with this.

Alternatively, ammend the for loop like this:

for row in doc_reader: 
    for i, ln in enumerate(lt): 
        ln.append(row[i])

for bigger data, i would prefer pandas which has vectorised methods.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your recommendation!! I'll try pandas

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.