Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
trouble with list array
#1
I have changed the code and I know I am creating a list. But when I go to print out list I get an error. Don't understand why. I know what it says at end of error, but doesn't make sense to me.

def load_track_to_Handi():
	my_tracklabel= Label(root, text ="                                                                               ", fg= "#F50808", font = ("sans_serif" , 16)).place(x=380, y=430)
	read_csvfile_into_array()
	
		
	global track_abrev
	global race_number
	global xx
	
	#if fileFound ==True:

		
		 
	sortedArr = []
	x=[]
	
	hcount = 0
	
	with open( 'C://guis/f_racefiles/'+ track_abrev + '.csv' , 'r') as race_file:

		csv_reader = csv.reader(race_file) 
		#for line in race_file: 
			#print(line)
		df = pd.read_csv(race_file, header =None)
		for i in df:
			#if int(np.array(df.iloc[hcount, 1:2 ])) < int(race_number):
			xx = np.array(df.iloc[hcount,18:19 ])
			#xx = (hcount,int(x))

			sortedArr.append(xx)
			hcount+=1
				                          				       		
		for i in range(0,len(sortedArr)):   	
			print(sortedArr[i])		
Error:
Milford@LAPTOP-3NUEM60C MINGW64 /c/guis
$ python testloop.py
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\Milford\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 1883, in __call__
return self.func(*args)
File "testloop.py", line 135, in select_track
load_track_to_Handi()
File "testloop.py", line 94, in load_track_to_Handi
xx = np.array(df.iloc[hcount,18:19 ])
File "C:\Users\Milford\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pandas\core\indexing.py", line 873, in __getitem__
return self._getitem_tuple(key)
File "C:\Users\Milford\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pandas\core\indexing.py", line 1443, in _getitem_tuple
self._has_valid_tuple(tup)
File "C:\Users\Milford\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pandas\core\indexing.py", line 702, in _has_valid_tuple
self._validate_key(k, i)
File "C:\Users\Milford\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pandas\core\indexing.py", line 1352, in _validate_key
self._validate_integer(key, axis)
File "C:\Users\Milford\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pandas\core\indexing.py", line 1437, in _validate_integer
raise IndexError("single positional indexer is out-of-bounds")
IndexError: single positional indexer is out-of-bounds

I don't understand why I'd have an index error.
Reply
#2
Take a look on the following code block:

    hcount = 0
     
    with open( 'C://guis/f_racefiles/'+ track_abrev + '.csv' , 'r') as race_file:
 
        csv_reader = csv.reader(race_file) 
        #for line in race_file: 
            #print(line)
        df = pd.read_csv(race_file, header =None)
        for i in df:
            #if int(np.array(df.iloc[hcount, 1:2 ])) < int(race_number):
            xx = np.array(df.iloc[hcount,18:19 ])
            #xx = (hcount,int(x))
 
            sortedArr.append(xx)
            hcount+=1
All these lines could be significantly simplified:

df = pd.read_csv("Path/to/your/csv/file.csv", header=None)
xx = df.iloc[:, 18].values
This is not an answer to your question, but your code definitely needs refactoring (use facilities provided by pandas). It is very suspicious, if you need to use pure Python loops when you're using pandas.
Reply
#3
Scidam,

Thank you so much. I cleaned up the code and now I can print the field I want to. Next is to be able to just pull the lines out that match a user input to one of the fields. I'll start working on that.

Thank You again.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  trouble reading string/module from excel as a list popular_dog 0 384 Oct-04-2023, 01:07 PM
Last Post: popular_dog
  functional LEDs in an array or list? // RPi user Doczu 5 1,522 Aug-23-2022, 05:37 PM
Last Post: Yoriz
  LIST or ARRAY Comparison and change of value nio74maz 0 1,669 Dec-21-2020, 05:52 PM
Last Post: nio74maz
  2d Array adds last element to entire list waiteup 2 2,025 Nov-19-2020, 08:25 PM
Last Post: bowlofred
  list trouble rediska 3 2,187 Oct-17-2020, 11:17 AM
Last Post: ibreeden
  Appending list Trouble Big Time Milfredo 7 3,024 Oct-01-2020, 02:59 AM
Last Post: Milfredo
  Trouble with converting list , dict to int values! faryad13 7 3,671 Sep-04-2020, 06:25 AM
Last Post: faryad13
  Finding an element in a 1d list in a 2d array lionrocker221 0 1,787 Jun-27-2020, 04:50 PM
Last Post: lionrocker221
  Make an array of string number in a List polantas 5 3,032 May-27-2020, 07:18 AM
Last Post: buran
  Trouble with list function Eggman72 2 1,712 Mar-23-2020, 09:36 PM
Last Post: Eggman72

Forum Jump:

User Panel Messages

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