Reverse Scoring in Python with Pandas

In this post, we will learn how to reverse scores in Python. Reversing questionnaire scores is easy; this post is concise.

Table of Contents

Why you Need to Reverse Code

We sometimes want to reverse the scoring when working with questionnaires that use a Likert scale (e.g., strongly disagree, disagree, neutral, agree, strongly agree).  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 on the telephone at the same time”). However, we often have negatively worded questions (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 to 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 a questionnaire’s scores means taking negatively worded questions and changing their coding, e.g., as one is recoded to 5.

Prerequisites

We can use the excellent Python package Pandas to reverse scores with Python. Here we will use Pandas DataFrame to read the data and reverse code the negatively worded items. 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 must 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 package 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, which 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)Code language: Python (python)
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)Code language: Python (python)

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

  • Save

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top
Share via
Copy link
Powered by Social Snap