Reputation: 2082
Given a pandas dataframe like the following
Q1 Q2 Q3
0 'A' 'B' 'A'
1 'B' 'B' 'C'
and a list containing values I want to compare against
['B','B','A']
How do I obtain for each row how many column entries coincided with the respective list entry? So the output should be some form of
[2/3, 2/3]
Little code snippet for the above:
import pandas as pd
df = pd.DataFrame({'Q1':['A','B'],'Q2':['B','B'],'Q3':['A','C']})
true_answers = ['B','B','A']
Upvotes: 1
Views: 229
Reputation: 863611
Compare values by list (it has to be same length like number of columns) and get mean
using DataFrame.eq
method:
s = df.eq(true_answers).mean(axis=1)
print (s)
0 0.666667
1 0.666667
dtype: float64
If want list use:
print (list(s))
[0.6666666666666666, 0.6666666666666666]
Detail:
print (df.eq(true_answers))
Q1 Q2 Q3
0 False True True
1 True True False
Upvotes: 4