Home > Blockchain >  Filling DataFrame with 1s and 0s
Filling DataFrame with 1s and 0s

Time:10-26

I have two pandas dataframes one is an answer key which looks like this

     0     1 2 3 4 5 6 7 8 9
 0 TTTTKEY B C B A D A D D C
 

The other dataframe has answers from students which looks like this

     0  1 2 3 4 5 6 7 8 9
0   182 C C C A D A D D C 
1   184 C C B A D C A D C

I am wondering how I could change the values to 1's if the answers from students match the answer key and change the values to 0's if the answers from students do not match the answer key. I think this could use a nested for loop but there might be a different way to to this.

CodePudding user response:

You can make use of np.where() for this

import numpy as np

equals = np.where(df1.values[:,1:] == df2.values[:,1:], 1, 0)
df2.iloc[:,1:] = equals
In [34]: df2
Out[34]: 
     0  1  2  3  4  5  6  7  8  9
0  182  0  1  0  1  1  1  1  1  1
1  184  0  1  1  1  1  0  0  1  1

CodePudding user response:

Simplest way is to do something like this:

for i in range(1, 9   1): # Assuming 9 is the last question number
    students[i] = students[i] == answers.loc[0][i]

Your resulting dataframe will contain True or False instead of 1s and 0s but I think it solves your problem

CodePudding user response:

assume dataframes are called df1 and df2, you can run

is_equal = df1.values[:,1:] == df2.values[:,1:]

is_equal will be numpy array with ones and zeros. You can convert it to 0s and 1s with is_equal.astype(int). You can wrap this in a dataframe, and add back in the 0 column if you wish.

  • Related