Nan
Nan

Reputation: 61

Filling DataFrame with 1s and 0s

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.

Upvotes: 2

Views: 301

Answers (4)

yudhiesh
yudhiesh

Reputation: 6799

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

import numpy as np

df2.iloc[:,1:] = np.where(df1.values[:,1:] == df2.values[:,1:], 1, 0)
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

Upvotes: 5

Muhammad Hassan
Muhammad Hassan

Reputation: 4229

Use:

cols = df_2.columns
df_2[cols[1:]] = df_2[cols[1:]].eq(df_1[cols[1:]].loc[0]).astype(int)

Output:

     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

Upvotes: 0

Riley
Riley

Reputation: 2261

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.

Upvotes: 0

Thomas Q
Thomas Q

Reputation: 890

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

Upvotes: -1

Related Questions