# Survey tool, code for negative scoring

**URL:** <https://discourse.psychopy.org/t/survey-tool-code-for-negative-scoring/9511>\
**Category:** Builder\
**Created:** [October 22, 2019, 10:20am UTC](https://discourse.psychopy.org/t/survey-tool-code-for-negative-scoring/9511 "2019-10-22T10:20:08Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![esandhu](https://avatars.discourse-cdn.com/v4/letter/e/e19adc/32.png) [@esandhu](https://discourse.psychopy.org/u/esandhu)\
**Post date:** [October 22, 2019, 10:20am UTC](https://discourse.psychopy.org/t/survey-tool-code-for-negative-scoring/9511/1 "2019-10-22T10:20:08Z")

</div>

If this template helps then use it. If not then just delete and start from scratch.

**OS** (e.g. Win10):  
**PsychoPy version** (e.g. 1.84.x): 3  
**Standard Standalone? (y/n)** If not then what?: y  
\*\*What are you trying to achieve?:use survey tool for PANAS questionnaire. The questions have a mixture of positve and negative scoring. Is there a way of coding this into survey tool (as one can do in ratingsscales component) so that the output data reflects this?

**What did you try to make it work?:**  
My excel file has a column ‘score’ which is either ‘+’ or ‘-’ I then put a code component at the end of the routine :  
if score ==’+’:  
thisExp.addData(‘scoring’, rating.getRating())  
elif score == ‘-’:  
thisExp.addData(‘scoring’, 5-rating.getRating())

\*\*What specifically went wrong when you tried that?:the error: … \PANAS\_lastrun.py", line 314, in   
if score ==’+’:  
NameError: name ‘score’ is not defined  
Include pasted full error message if possible. “That didn’t work” is not enough information.

---

<div class="post-metadata">

**Author:** ![dvbridges](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/dvbridges/32/1918_2.png) [@dvbridges](https://discourse.psychopy.org/u/dvbridges)\
**Post date:** [October 22, 2019, 10:24am UTC](https://discourse.psychopy.org/t/survey-tool-code-for-negative-scoring/9511/2 "2019-10-22T10:24:31Z")

</div>

Hi @esandhu , you will need to create a column called “score” in your excel conditions file, and feed that file into the loop.

---

<div class="post-metadata">

**Author:** ![esandhu](https://avatars.discourse-cdn.com/v4/letter/e/e19adc/32.png) [@esandhu](https://discourse.psychopy.org/u/esandhu)\
**Post date:** [October 22, 2019, 10:48am UTC](https://discourse.psychopy.org/t/survey-tool-code-for-negative-scoring/9511/3 "2019-10-22T10:48:18Z")

</div>

Thank you for your, as always, swift response. I have already created a column called “score” in my excel conditions file. The headings for my condition file are: questionText options score layout type questionWidth responseWidth questionColor responseColor  
Is the survey tool set up to allow this?

---

<div class="post-metadata">

**Author:** ![dvbridges](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/dvbridges/32/1918_2.png) [@dvbridges](https://discourse.psychopy.org/u/dvbridges)\
**Post date:** [October 22, 2019, 11:15am UTC](https://discourse.psychopy.org/t/survey-tool-code-for-negative-scoring/9511/4 "2019-10-22T11:15:20Z")

</div>

I see, and you are using the Form component. This works different from your usual loop and simply adding a score column to the form will not work, it will just be ignored. Having negative scoring options is something that can be added to the form, so thanks for highlighting this issue. In the mean time, you need to change the scores yourself at the end of the routine. Here is some code to help:

```auto
# End Routine
import pandas as pd
newData = pd.DataFrame.from_dict(form.getData())
newData = newData.sort_values(by=["itemIndex"])

answers = pd.read_csv("PANASFILE.csv")
newData['ratings'] = np.where(answers['score']=='-', 5 - newData['ratings'], newData['ratings'])
newData.to_csv("reverseScoresdData.csv")

```

---

<div class="post-metadata">

**Author:** ![esandhu](https://avatars.discourse-cdn.com/v4/letter/e/e19adc/32.png) [@esandhu](https://discourse.psychopy.org/u/esandhu)\
**Post date:** [October 22, 2019, 4:12pm UTC](https://discourse.psychopy.org/t/survey-tool-code-for-negative-scoring/9511/5 "2019-10-22T16:12:05Z")

</div>

Thank you, that is now working. I added:  
newData[‘participant’] = expInfo[‘participant’]  
so that the partcipant number is saved alongside the reverse scores.  
The only issue is that the file name is constant and not unique. Is pandas able to draw data from psychopy so the file name can incorporate the participant number?  
Best wishes

---

<div class="post-metadata">

**Author:** ![dvbridges](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/dvbridges/32/1918_2.png) [@dvbridges](https://discourse.psychopy.org/u/dvbridges)\
**Post date:** [October 22, 2019, 5:04pm UTC](https://discourse.psychopy.org/t/survey-tool-code-for-negative-scoring/9511/6 "2019-10-22T17:04:58Z")

</div>

#Sure, you could use:

```python
newData.to_csv("{}_reverseScoresdData.csv".format(expInfo['participant']))

```

This will create a file name, such as the following,if your participant is labelled “1”

```python
"1_reverseScoresdData.csv"

```

---

<div class="post-metadata">

**Author:** ![esandhu](https://avatars.discourse-cdn.com/v4/letter/e/e19adc/32.png) [@esandhu](https://discourse.psychopy.org/u/esandhu)\
**Post date:** [October 22, 2019, 5:09pm UTC](https://discourse.psychopy.org/t/survey-tool-code-for-negative-scoring/9511/7 "2019-10-22T17:09:13Z")

</div>

That’s great. Thank you

---

<div class="post-metadata">

**Author:** ![rdkirkden](https://avatars.discourse-cdn.com/v4/letter/r/848f3c/32.png) [@rdkirkden](https://discourse.psychopy.org/u/rdkirkden)\
**Post date:** [January 31, 2022, 3:18pm UTC](https://discourse.psychopy.org/t/survey-tool-code-for-negative-scoring/9511/8 "2022-01-31T15:18:49Z")

</div>

Thank you for the neat code solution, @dvbridges!

In case anybody just wants to calculate and report a total score from the forward and reversed ratings, as opposed to reporting the individual forward and reversed ratings themselves, it is easy to write the score to the main results file, instead of to a new csv file.

```auto
# Read in responses
newData = pd.DataFrame.from_dict(form.getData())
# Read in values from conditions file
answers = pd.read_csv("PANASFILE.csv")
# Apply forward and reverse scoring to responses
newData['ratings'] = np.where(answers['score']=='-', 5 - newData['response'], newData['response'])
# Calculate total score
scoreTotal = newData['ratings'].sum()
# Save score to data file
thisExp.addData("TotalScore", scoreTotal)

```

The score is written to the first row of form data in the results file. I don’t think the ‘thisExp.addData’ method gives write access to subsequent rows of form data in the results file, so this approach is not useful if you want to report the individual forward and reversed ratings in a tidy way. Better to create a new csv file, as proposed by @dvbridges.
