0

I am trying to compare two columns (key.response and corr_answer) in a csv file using pandas and creating a new column "Correct_or_Not" that will contain a 1 in the cell if the key.response and corr_answer column are equal and a 0 if they are not. When I evaluate on their own outside of the loop they return the truth value I expect. The first part of the code is just me formatting the data to remove some brackets and apostrophes.

I tried using a for loop, but for some reason it puts a 0 in every column for 'Correct_or_Not".

import pandas as pd
df= pd.read_csv('exptest.csv')
df['key.response'] = df['key.response'].str.replace(']','')
df['key.response'] = df['key.response'].str.replace('[','')
df['key.response'] = df['key.response'].str.replace("'",'')
df['corr_answer'] = df['corr_answer'].str.replace(']','')
df['corr_answer'] = df['corr_answer'].str.replace('[','')
df['corr_answer'] = df['corr_answer'].str.replace("'",'')

for i in range(df.shape[0]):
    if df['key.response'][i] == df['corr_answer'][i]:
        df['Correct_or_Not']=1
    else: 
        df['Correct_or_Not']=0

df.head()

  key.response corr_answer Correct_or_Not
0   1              1            0
1   2              2            0
2   1              2            0
1
  • 2
    the problem is df['Correct_or_Not']=1 creates a new column of 1s from scratch each time (same with df['Correct_or_Not']=0) Commented Nov 12, 2019 at 15:09

2 Answers 2

2

You can generate the Correct_or_Not column all at once without the loop:

df['Correct_or_Not'] = df['key.response'] == df['corr_answer']

and df['Correct_or_Not'] = df['Correct_or_Not'].astype(int) if you need the results as integers.

In your loop you forgot the index [i] when assigning the result. Like this the last row's result gets applied everywhere.

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

Comments

0

you can also do this

df['Correct_or_not']=0
    for i in range(df.shape[0]):
        if df['key.response'][i]==df['corr_answer'][i]:
            df['Correct_or_not'][i]=1

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.