0

I have a list of strings (longer than in this example). If one of the strings exists in a row of data, I want to skip that row. This is what I have so far but I get an index error, which leads me to believe I'm not looping correctly.

stringList = ["ABC", "AAB", "AAA"]

with open('filename.csv', 'r')as csvfile:
    filereader = csv.reader(csvfile, delimiter=',')
    next(filereader, None) #Skip header row
    for row in filereader:
        for k in stringList:
            if k not in row:
                data1 = column[1] 

The error I get: IndexError: list index out of range. I realize I'm reading by row, but I need to extract the data by column.

3
  • 1
    I don't see column assigned anywhere. Commented Apr 8, 2019 at 19:48
  • If your CSV has headers I recommend using DictReader Commented Apr 8, 2019 at 19:49
  • 1
    replace data1 = column[1] by data1 = row[1]. if required add check for len(row)>1 Commented Apr 8, 2019 at 19:49

2 Answers 2

1

The error is because row is a list and you are using/accessing it as a normal variable. You can access certain columns by using appropriate indexing of the list row. Eg: in the first iteration row[0] will be the element in the first-row first-column, row[1] the second column entry and so on. On subsequent iterations of row, you can access entries of subsequent column downwards.

Here's a simple loop to do it.

for row in filereader:
        for k in stringList:
            for i in range(len(row)):
                if k not in row[i]:
                    someVar=row[i]
Sign up to request clarification or add additional context in comments.

3 Comments

This results in an error: TypeError: 'int' object is not iterable
Did you mean range(len(row))?
Yes indeed, sorry for that. I've updated the answer.
1

With pandas you can do it easily, with a mask. See more: link

import pandas as pd

data = pd.read_csv('filename.csv')
data = data.loc[data['column_name'] not in stringList]

Comments

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.