Jul-12-2023, 05:05 PM
(This post was last modified: Jul-12-2023, 05:20 PM by deanhystad.)
Looking at your code, perc is Pandas series, not a list. Indexing for a series does not work like indexing in a list. Indexing for a series works more like indexing in a dictionary. The index value is a key, not a position. The error message would be
But I don't get an error when I run your code. I get this.
In the future please post the entire error message, including the trace.
Error:KeyError: -2
This error will occur if you have a question that contains no "Strongly Disagree" type.But I don't get an error when I run your code. I get this.
Output: question type value percentage percentage_end percentage_start
0 Question 1 Strongly disagree 24 0.746501 -18.382582 -19.129082
1 Question 1 Disagree 294 9.144635 -9.237947 -18.382582
2 Question 1 Neither agree nor disagree 594 18.475894 9.237947 -9.237947
3 Question 1 Agree 1927 59.937792 69.175739 9.237947
4 Question 1 Strongly agree 376 11.695179 80.870918 69.175739
5 Question 2 Strongly disagree 2 9.090909 -9.090909 -18.181818
6 Question 2 Disagree 2 9.090909 0.000000 -9.090909
7 Question 2 Neither agree nor disagree 0 0.000000 0.000000 0.000000
8 Question 2 Agree 7 31.818182 31.818182 0.000000
9 Question 2 Strongly agree 11 50.000000 81.818182 31.818182
10 Question 3 Strongly disagree 2 20.000000 -10.000000 -30.000000
11 Question 3 Disagree 0 0.000000 -10.000000 -10.000000
12 Question 3 Neither agree nor disagree 2 20.000000 10.000000 -10.000000
13 Question 3 Agree 4 40.000000 50.000000 10.000000
14 Question 3 Strongly agree 2 20.000000 70.000000 50.000000
15 Question 4 Strongly disagree 0 0.000000 -15.625000 -15.625000
16 Question 4 Disagree 2 12.500000 -3.125000 -15.625000
17 Question 4 Neither agree nor disagree 1 6.250000 3.125000 -3.125000
18 Question 4 Agree 7 43.750000 46.875000 3.125000
19 Question 4 Strongly agree 6 37.500000 84.375000 46.875000
20 Question 5 Strongly disagree 0 0.000000 -10.416667 -10.416667
21 Question 5 Disagree 1 4.166667 -6.250000 -10.416667
22 Question 5 Neither agree nor disagree 3 12.500000 6.250000 -6.250000
23 Question 5 Agree 16 66.666667 72.916667 6.250000
24 Question 5 Strongly agree 4 16.666667 89.583333 72.916667
25 Question 6 Strongly disagree 1 6.250000 -12.500000 -18.750000
26 Question 6 Disagree 1 6.250000 -6.250000 -12.500000
27 Question 6 Neither agree nor disagree 2 12.500000 6.250000 -6.250000
28 Question 6 Agree 9 56.250000 62.500000 6.250000
29 Question 6 Strongly agree 3 18.750000 81.250000 62.500000
30 Question 7 Strongly disagree 0 0.000000 -10.000000 -10.000000
31 Question 7 Disagree 0 0.000000 -10.000000 -10.000000
32 Question 7 Neither agree nor disagree 1 20.000000 10.000000 -10.000000
33 Question 7 Agree 4 80.000000 90.000000 10.000000
34 Question 7 Strongly agree 0 0.000000 90.000000 90.000000
35 Question 8 Strongly disagree 0 0.000000 0.000000 0.000000
36 Question 8 Disagree 0 0.000000 0.000000 0.000000
37 Question 8 Neither agree nor disagree 0 0.000000 0.000000 0.000000
38 Question 8 Agree 0 0.000000 0.000000 0.000000
39 Question 8 Strongly agree 2 100.000000 100.000000 0.000000
Are you sure the error you are getting is associated with the code in your post?In the future please post the entire error message, including the trace.