I have a "results" dataframe with values, like so
Symbol Close MACD MACD signal MACD_CrossUp MACD_CrossDown diffMACD K ... diffKDJ KDJ_buy KDJ_sell Kalman Choppy Long Short Tick
0 STMX/USDT 0.00966 -0.000024 -0.000044 True False 0.000020 77.608368 ... 50.460498 True False True 56.28 True False STMX
1 DYDX/USDT 1.66200 0.002000 0.000198 True False 0.001802 73.642217 ... 24.518652 True False True 59.22 True False DYDX
2 HBAR/USDT 0.08807 0.000033 -0.000138 True False 0.000171 82.441072 ... 23.431535 True False True 48.73 True False HBAR
3 FLM/USDT 0.11750 0.000126 -0.000468 True False 0.000594 66.179365 ... 21.874289 True False True 59.20 True False FLM
4 RVN/USDT 0.02738 -0.000009 -0.000033 True False 0.000023 72.490969 ... 17.821932 True False True 65.48 True False RVN
5 BNX/USDT 131.14000 -0.002547 -0.163706 True False 0.161159 83.749361 ... 16.006878 True False True 62.13 True False BNX
6 XMR/USDT 186.96000 0.371050 0.203506 True False 0.167544 79.540010 ... 14.986170 True False True 57.52 True False XMR
7 XEM/USDT 0.05010 0.000006 -0.000056 True False 0.000063 67.637342 ... 14.427833 True False True 59.89 True False XEM
8 XTZ/USDT 2.11000 0.006027 0.003346 True False 0.002681 85.457781 ... 13.049522 True False True 55.48 True False XTZ
9 RLC/USDT 0.90310 0.002712 -0.000123 True False 0.002835 89.556189 ... 8.648385 True False True 38.42 True False RLC
The idea that I need to solve is that it sorts on various fields, but in some cases I need them to be in ascending order and others in descending order.
Right now I have these two sorts:
df_top = results.sort_values( ['Long', 'diffKDJ', 'diffMACD', 'Choppy'],ascending=False)
df_bottom = results.sort_values( ['Short', 'diffKDJ', 'diffMACD', 'Choppy'],ascending=False)
Therefore, my results are sorted by locating, first, all the Long, and among them, those with the highest "diffKDJ", and if some values coincide, I would reorder them by "diffMACD" and the same case for "choppy".
But what if I need you to do this?
- First group all "Long" = True (This sorting is already done well with the df_top = results.sort_values()
- Now that I have all the "Long" = TRUE sorted, I want these same records to come out sorted by the "diffMACD" field, putting the one closest to 0 first, that is, sorting in ASCENDING
- And in the event that there are two or more records whose "diffMACD" is identical, in this case it would be necessary to order by "diffKDJ" also in ASCENDING
As an example, I put the one above of how I would like it to come out. I leave the indices like the one above to make it easier to see the reordering
Symbol Close MACD MACD signal MACD_CrossUp MACD_CrossDown diffMACD K ... diffKDJ KDJ_buy KDJ_sell Kalman Choppy Long Short Tick
0 STMX/USDT 0.00966 -0.000024 -0.000044 True False 0.000020 77.608368 ... 50.460498 True False True 56.28 True False STMX
4 RVN/USDT 0.02738 -0.000009 -0.000033 True False 0.000023 72.490969 ... 17.821932 True False True 65.48 True False RVN
7 XEM/USDT 0.05010 0.000006 -0.000056 True False 0.000063 67.637342 ... 14.427833 True False True 59.89 True False XEM
2 HBAR/USDT 0.08807 0.000033 -0.000138 True False 0.000171 82.441072 ... 23.431535 True False True 48.73 True False HBAR
3 FLM/USDT 0.11750 0.000126 -0.000468 True False 0.000594 66.179365 ... 21.874289 True False True 59.20 True False FLM
1 DYDX/USDT 1.66200 0.002000 0.000198 True False 0.001802 73.642217 ... 24.518652 True False True 59.22 True False DYDX
8 XTZ/USDT 2.11000 0.006027 0.003346 True False 0.002681 85.457781 ... 13.049522 True False True 55.48 True False XTZ
9 RLC/USDT 0.90310 0.002712 -0.000123 True False 0.002835 89.556189 ... 8.648385 True False True 38.42 True False RLC
5 BNX/USDT 131.14000 -0.002547 -0.163706 True False 0.161159 83.749361 ... 16.006878 True False True 62.13 True False BNX
6 XMR/USDT 186.96000 0.371050 0.203506 True False 0.167544 79.540010 ... 14.986170 True False True 57.52 True False XMR
Based on @HeytalePazguato's answer:
Solved like this:
The ascending argument can receive a list, so you could put [False, True, True]