sorting

Order Django queryset by value closest to 0

Order Django queryset by value closest to 0 Question: I have a queryset that is returning a list of entries that are tied for the lead in a contest… leaders = ContestEntry.objects.filter(name=game, wins=wins) After I get this data I need to put them in order by a second field (‘difference’) as a tie breaker. The …

Total answers: 2

Rearrange the columns of a pandas DataFrame based on row number

Rearrange the columns of a pandas DataFrame based on row number Question: There is a dataframe: import pandas as pd df = pd.DataFrame(data = {‘a’:[3,0,2,1],’b’:[4,3,2,1],’c’:[3,2,1,0],’d’:[4,3,2,0]}) print(df) >>> df a b c d 0 3 4 3 4 1 0 3 2 3 2 2 2 1 2 3 1 1 0 0 How to rearrange(sort?) …

Total answers: 3

How can I sort elements of list in function?

How can I sort elements of list in function? Question: Why I got: None None def odd_even(*number): even = [] odd = [] for i in number: if i % 2 == 0: even.append(i) else: odd.append(i) print(even.sort()) print(odd.sort()) odd_even(1,5,7,8,4,3,26,59,48,7,5,45) I just tried sorting elements of list But result is None None Asked By: Necip Arda …

Total answers: 2

Can python list sort key function take index instead of an element?

Can python list sort key function take index instead of an element? Question: Take a look at this code: points = […] properties = [compute_point_property(point) for point in points] points.sort(?) Here I have a list of points, and I compute a property of each point. Then I need to sort the points by their corresponding …

Total answers: 1

Re-sort string based on precedence of separator

Re-sort string based on precedence of separator Question: I have a string with a certain meaning for example "a,b;X1" or e&r1. In total there are 3 possible separators between values: ,;& where ; has low precedence. Also "a,b;X1" and "b,a;X1"are the same but to be able to compare they are the same, I want to …

Total answers: 3

Looping using array name?

Looping using array name? Question: Im trying to loop over arrays with similar name, as such: a0=[] a1=[] a2=[] a3=[] a4=[] for k in range(len(mlac)): # mlac.shape (17280, 5) for I in range(len(mlac[0])): ‘a%i’%I.append(mlac[k][I]) # or : ‘a{}’.format(I).append(mlac[k][I]) But both are considered strings on the loop and cannot append. So for each iteration of ‘I’, …

Total answers: 3

Get last actual data

Get last actual data Question: There is a table rule_chains: ID payroll_type_id stuff_department_id start_date 1 1 89 2023-03-01 2 2 89 2023-03-01 3 1 89 2023-04-01 My problem is to get the actual entity for today. For example today is 2023-03-01 and I need to get this two entity with id = 1 and id …

Total answers: 1

Pandas want to change column data into serial number

Pandas want to change column data into serial number Question: I want to change the "Dut" column into a serial number following the number of each "ITEM" There are lots of "ITEM"s. So I want to get the number of each item. as following question Input data: data = {‘ITEM’: [‘a’, ‘a’, ‘a’, ‘a’, ‘a’, …

Total answers: 2