To plot a waterfall, I create three lists as input: the value, the bar text, the axis text. My goal is to sort every number in between the first and last element ascending, i.e. exclude the first and last element in the list from being sorted.
I have three lists containing following elements:
A = [50000,8000,-3000,-4000,51000]
B = ['Start','Profit1','Cost2','Cost1','End']
C = ['50K','8k','-3k','-4k','51k']
Using the suggested method here (sort values in list by other list), I state:
a_new = sorted(A)
b_new = [x for _, x in sorted(zip(A, B))]
c_new = [x for _, x in sorted(zip(A, C))]
print(a_new)
print(b_new)
print(c_new)
And receive as Output:
[-4000, -3000, 8000, 50000, 51000]
['Cost1', 'Cost2', 'Profit1', 'Start', 'End']
['-4k', '-3k', '8k', '50K', '51k']
Though, my goal is to fix the first and the last element of the list and sort all other elements only. Desired:
[50000,-4000,-3000,8000,51000]
['Start','Cost1', 'Cost2', 'Profit1','End']
['50K','-4k', '-3k', '8k','51k']
I think I am close to the solution, but have been stuck here for quite some time...
You can use np.argsort on the portion you want:
order = np.r_[0,np.argsort(A[1:-1]) + 1, len(A)-1]
a_new = np.array(A)[order]
b_new = np.array(B)[order]
c_new = np.array(C)[order]
Output:
[50000 -4000 -3000 8000 51000]
['Start' 'Cost1' 'Cost2' 'Profit1' 'End']
['50K' '-4k' '-3k' '8k' '51k']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With