trouble with list array - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: trouble with list array (/thread-29678.html) |
trouble with list array - Milfredo - Sep-15-2020 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. RE: trouble with list array - scidam - Sep-15-2020 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+=1All these lines could be significantly simplified: df = pd.read_csv("Path/to/your/csv/file.csv", header=None) xx = df.iloc[:, 18].valuesThis 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. RE: trouble with list array - Milfredo - Sep-16-2020 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. |