Python Forum

Full Version: trouble with list array
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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.
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.
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.