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:
[email protected] 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
  LIST or ARRAY Comparison and change of value nio74maz 0 478 Dec-21-2020, 05:52 PM
Last Post: nio74maz
  2d Array adds last element to entire list waiteup 2 573 Nov-19-2020, 08:25 PM
Last Post: bowlofred
  list trouble rediska 3 798 Oct-17-2020, 11:17 AM
Last Post: ibreeden
  Appending list Trouble Big Time Milfredo 7 1,021 Oct-01-2020, 02:59 AM
Last Post: Milfredo
  Trouble with converting list , dict to int values! faryad13 7 1,341 Sep-04-2020, 06:25 AM
Last Post: faryad13
  Finding an element in a 1d list in a 2d array lionrocker221 0 721 Jun-27-2020, 04:50 PM
Last Post: lionrocker221
  Make an array of string number in a List polantas 5 1,207 May-27-2020, 07:18 AM
Last Post: buran
  Trouble with list function Eggman72 2 696 Mar-23-2020, 09:36 PM
Last Post: Eggman72
  append list to empty array SchroedingersLion 1 1,036 Feb-02-2020, 05:29 PM
Last Post: SchroedingersLion
  How to read a text file into a list or an array musouka 2 1,616 Oct-07-2019, 01:54 PM
Last Post: musouka

Forum Jump:

User Panel Messages

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