Python Forum
Negative indexing/selecting working and not working
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Negative indexing/selecting working and not working
#3
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
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.
Reply


Messages In This Thread
RE: Negative indexing/selecting working and not working - by deanhystad - Jul-12-2023, 05:05 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Gmpy2 Newbie Working on Precision charlesrkiss 6 813 Jun-14-2024, 09:53 AM
Last Post: sunflower36002
  Working with group of lines knob 1 321 May-21-2024, 07:21 AM
Last Post: Gribouillis
  two functions working in a strange way zapad 2 393 May-02-2024, 01:35 PM
Last Post: zapad
  Excel isnt working properly after python function is started IchNar 2 447 May-01-2024, 06:43 PM
Last Post: IchNar
  negative memory usage akbarza 1 376 Apr-27-2024, 08:43 AM
Last Post: Gribouillis
  Python trivial endgame engine is not working as expected max22 0 648 Feb-24-2024, 04:41 PM
Last Post: max22
  File Handling not working properly TheLummen 8 1,108 Feb-17-2024, 07:47 PM
Last Post: TheLummen
  Spyder console zoom in not working? Dionysis 2 598 Feb-06-2024, 03:31 PM
Last Post: paul18fr
  SendKeys not working SanjayGMusafir 4 685 Jan-16-2024, 12:07 PM
Last Post: EdwardMatthew
  Text conversion to lowercase is not working ineuw 3 626 Jan-16-2024, 02:42 AM
Last Post: ineuw

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020