In this post, we are going to learn how to reverse scores in Python.

When working with questionnaires that use a Likert scale (e.g., strongly disagree, disagree, neutral, agree, strongly agree) we sometimes want to reverse the scoring.  For instance, in a questionnaire subjective experiences of cognition some of the questions can be positively worded (e.g., “I find it easy to read while talking in the telephone at the same time”). However, we often have questions that are negatively worded (e.g., “When there is music in the room I find it hard to concentrate on reading”).

  • Save
Data to reverse in Python

In the case of positively worded questions we can score them like this; strongly disagree with a score of 1, disagree= 2, neutral =3, agree = 4 and strongly disagree =5. However, the same scoring can’t be applied for the negatively worded questions. We now need to reverse the score of these questions.  In the python example below, score 5 becomes 1, 4 becomes 2, 2 becomes 4 and 1 becomes 5. Three, on the other hand, (neutral) stays the same.

The following Python example uses Pandas DataFrame. If you need to learn more about Pandas check out my dataframe tutorial.

Reverse scoring in Python:

In this reverse scoring with Python example, we are generating some data. Typically, we have collected data using an instrument and the data can be either in Excel (e.g., xlsx) or CSV format. If you need to learn more about loading Excel to Dataframes see my Pandas read_excel guide.

from random import randint
import pandas as pd

def reverseScoring(df, high, cols):
    '''Reverse scores on given columns
     df = your data frame,
     high = highest score available
     cols = the columns you want reversed in list form'''
    df[cols] = high - df[cols]
    return df

#Generate column names for the DataFrame (Question 1; Q1, and so on) 
colNames = ['Q' + str(i) for i in range(1,6)]

#Generating data for the DataFrame
data = [[randint(1,4) for i in xrange(5)] for i in xrange(1,11)]

#Finally generating the DataFrame using Pandas 
dataf = pd.DataFrame(data, index=range(1,11), columns=colNames)

#The Columns to be reversed
cols = ['Q2', 'Q3', 'Q4']

#Create a new DataFrame with reversed scores
revFrame = reverseScoring(dataf, 5, cols)
Reverse scoring in Python
  • Save
Reversed columns and values
  • See this Jupyter Notebook for a Python 3 code example.

How to Save a Pandas Dataframe

With Pandas DataFrame you can easily save your new dataframe to .csv-file. I use ‘;’ as separator (‘,’ is more commonly used):

#Saving your new dataframe as a csv (YOUR_FILENAME='filename.csv 
to_csv(path_or_buf=YOUR_FILENAME, sep=CHOSEN_SEPARATOR)

That is it! Pretty simple to reverse scores in Python. Now you can continue with your analysis.

More on how to work with Pandas Dataframes:

Update: If you are using R I have written a short script on how to reverse scores using that language: Reverse scoring in R statistical programming environment

reverse scores
  • Save
Share via
Copy link
Powered by Social Snap