1

I read a csv file named df, which has 4 columns(id, skill,..) and 30 rows, I want the program to check each id and if the id is "A035755" then print its related skill (j). I wrote the following program it almost works but the problem is it iterates more than once so instead of 30 results I have more than 100!
I think my indexing might be the problem but not sure how to fix it.

Thanks J

df=pd.read_csv('location')
for i in df.id[0:len(df)]:  #id is one of the columns
    for j in df.skill:       #skill another column
        if i == "A035755":
            print(j)
        else:
            print("Not Found")

2 Answers 2

1

If I understand correctly, df's attributes (df.id, df.skill) represent columns and indexes in the lists (df.id[1..n]) represent rows, then proper way is following:

df = pd.read_csv('location')
for index, rec_id in enumerate(df.id):  #id is one of the columns
    if rec_id == "A035755":
        print(df.skill[index])
Sign up to request clarification or add additional context in comments.

Comments

0

So you can iterate and refrence the index. Like this

for index,value in df.id.items():  #id is one of the columns
        if value == "A035755":
            print(df.skill[index])
        else:
            print("Not Found")

Or you can iterate on records

for record in df:
    if record.id == "A035755":
        print(record.skill)
    else:
        print("Not Found")

2 Comments

Thanks, But none of the solution works. Actually what the program does, is printing skill column 3 times and then one time "Not found"
can you add present output and expected output to the question?

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.