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

Why you Need to Reverse Code

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

What is Reverse Scoring?

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.

What does it Mean to Reverse Score?

To reverse scores, of a questionnaire, therefore means that you take negatively worded questions and change the coding of them e.g. such as 1 is recoded to 5.

Prerequisites

To reverse scores with Python, we can use the excellent Python package Pandas. Here we will use Pandas DataFrame to read the data and reverse code the negatively worded items. If you need to learn more about Pandas check out my dataframe tutorial. If you need to install Pandas you can use Pip: pip install pandas. Using pip is an excellent method to install Python packages but, sometimes, you need to upgrade pip to the latest version. Finally, before going on to the reverse scoring example, it is worth mentioning that you can use pip to install a specific version of a Python pacakge as well.

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 reverse 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 # & CHOSEN_SEPARATOR=',') 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