dazai
dazai

Reputation: 716

How do I check if two columns match base on them having the same string?

I'm trying to analyze a dataset I have that looks like this:

 PRESSEDKEY     PALABRA   COLOR  KEYCORR      RT
     V           HORNO     blue     a        198

Basically, I need to check if the pressed key matches the correct key (the correct key is in lower case but that doesn't matter). So here the pressed key was V instead of A so the result is incorrect. The options are A, V and R. I also need to see the difference in reaction time between the correct and incorrect times once I get it.

How do I manage to do this?

I thought of this but this doesn't work and might be too ardous to try with each key unless that's the only way to do it:

pressed_key = filtered_data['PRESSEDKEY']
keycorr = filtered_data['KEYCORR']

def correctV(pressed_key, keycorr):
   if pressed_key.str.contains('V'):
       if keycorr.str.contains('v'):
           print('Correct')
       else:
           print('Incorrect')

Upvotes: 0

Views: 420

Answers (1)

sophocles
sophocles

Reputation: 13821

One way you could go about it is to create a column that will show whether PRESSEDKEY equals KEYCORR, and then get the mean RT (reaction time) per group:

# Assign Y/N
df.loc[df['PRESSEDKEY'].str.lower() == df['KEYCORR'],'correct'] ='Y'
df.fillna('N',inplace=True)

# Compare RT
df.groupby('correct')['RT'].mean() 

>>> df

  PRESSEDKEY PALABRA COLOR KEYCORR   RT correct
0          V   HORNO  blue       a  198       N
1          R   HORNO  blue       r  231       Y
2          A   HORNO  blue       a  325       Y
3          V   HORNO  blue       v  111       Y
4          V   HORNO  blue       a  231       N
5          R   HORNO  blue       r  213       Y
6          A   HORNO  blue       v  251       N
7          V   HORNO  blue       a  198       N


>>> df.groupby('correct')['RT'].mean() 

correct
N    219.5
Y    220.0
Name: RT, dtype: float64

Upvotes: 1

Related Questions