Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Trouble with Sort
#1
I am having trouble getting sort to work.

Here is the code

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
	
	
	#if fileFound ==True:

		
		
	
	x=[]
	xx = []
	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:
			x= np.array(df.iloc[hcount, 17:18])
			xx = (hcount,int(x))
			#print (xx)
			hcount+=1
			print(xx)
		
		np.sort(x, axis=1)
Here is what I get. I want to sort the 2nd column....

(0, 3)
(1, 10)
(2, 8)
(3, 12)
(4, 2)
(5, 1)
(6, 4)
(7, 8)
(8, 10)
(9, 1)
(10, 1)
(11, 6)
(12, 10)
(13, 4)
(14, 3)
(15, 5)
(16, 2)
(17, 12)
(18, 4)
(19, 4)
(20, 10)
(21, 6)
(22, 12)
(23, 6)
(24, 3)
(25, 15)
(26, 2)
(27, 5)
(28, 4)
(29, 2)
(30, 10)
(31, 2)
(32, 20)
(33, 6)
(34, 3)
(35, 3)
(36, 4)
(37, 12)
(38, 6)
(39, 4)
(40, 10)
(41, 10)
(42, 4)
(43, 6)
(44, 3)
(45, 30)
(46, 10)
(47, 12)
(48, 3)
(49, 8)
(50, 6)
(51, 4)
(52, 10)
(53, 6)
(54, 30)
(55, 5)
(56, 6)
(57, 10)
(58, 10)
(59, 20)
(60, 8)
(61, 6)
(62, 15)
(63, 12)


Thank you.
Reply
#2
Can you also post the sample file you're reading the data from?

In the meantime, I think this code can help you figure this out:
#Original data
data =((1,19),(2,10),(3,4),(2,7))
#Built-in sort function
print (sorted(data))
#define new sort criteria
print(sorted(data,key = lambda e: e[1]))
Reply
#3
I don't understand. you want to see the inside of the file? It's pretty big. Here is one record of the 63



['AP ' 8 'D' 'C' 10000 37 6 28 4 61 7 149 14 159 20 280 29 12.0
'JIVE DADDY' 13 4 0 5 0 24 5 8 2 2020 2 2019 10 45 8 20.0 72.0 1100.0
1210.0 1320.0 1210.0 1100.0 990.0 1100.0 1100.0 1100.0 1100.0 'D' 'D' 'D'
'D' 'T' 'D' 1100.0 'D' 'T' 'D' 7.0 10.0 9.0 11.0 12.0 8.0 9.0 6.0 10.0
7.0 1.0 5.0 2.0 1.0 2.0 2.0 4.0 1.0 3.0 4.0 1.0 5.0 3.0 1.0 2.0 1.0 3.0
1.0 3.0 5.0 3.0 60.0 51.0 33.0 89 89 89 87 0
'FOR THREE YEAR OLDS AND UPWARD WHICH HAVE NOT WON A RACE SINCE JANUARY 1; 2020 OR WHICH HAVE NEVER WON FOUR RACES. Three Year Olds; 121 lbs.; Older; 124 lbs. Claiming Price $4;000 (Races Where Entered For $3;200 Or Less Not Considered In Eligibility).'
'Clm 4000']


Hope this is what you asked for.
Reply
#4
Pandas has rich facilities for sorting. Did you read about sort_values method.
Reply
#5
I will do more research. Thank you.

XX is a two dimension array. i column and 63 rows. I used this code and it threw an error. Which is posted under the code.
unsorted_list = xx
	sorted_list = sorted(unsorted_list, key=lambda x:x[0])
	print(sorted_list)	
Error.....

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 126, 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 what this all means.
Reply
#6
It seems that you use Numpy arrays. I don't think built-in function will give you what you expect. I would create a list out of the array. Please have a look at the following code.
import numpy as np

A = np.array([[1,3],[6,4],[5, 0]])
print(A)

print('Sorted is: ')
print(np.sort(A))

# lista = []

# for i in range (len(A)):
#     lista.append(A[i])

# print (lista)
# B = sorted(lista, key = lambda e:e[1])
# print(B)
Reply
#7
Askic, When I put your code in my program it works like it's supposed to. But when I substitute my array in place of your array it throws an error. Only difference I can see, is you array 1 row with three columns. My array is 63 rows and 1 column.

So I just don't know what to do.
Reply
#8
First thing that cross my mind is zip and transpose. Please have a look at this:
zip
Reply
#9
I know it must be a mistake I am making. I will start over and make sure my array creation is right. Thanks
Reply


Forum Jump:

User Panel Messages

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