Adding to the solution using piRSquared: This shifts all values ββto the left , not up.
If not all values ββare numbers, use pd.isnull
v = df.values a = [[n]*v.shape[1] for n in range(v.shape[0])] b = pd.isnull(v).argsort(axis=1, kind = 'mergesort')
A little explanation:
a is a list of the length of v.shape[0] , and it looks something like this:
[[0, 0, 0, 0], [1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3], [4, 4, 4, 4], ...
what happens here is v - m x n , and I did both a and b m x n , and so we do each entry i,j in a and b to get an element in the row with the element value at i,j in a and the column with the value of the element at i,j , in b . Therefore, if we have a and b , both look like the above matrix, then v[a,b] returns a matrix in which the first row contains n copies of v[0][0] , the second row contains n copies of v[1][1] and so on.
In the solution piRSquared, its i is a non-matrix list. Thus, the list is used for v.shape[0] times, aka once for each line. Similarly, we could do:
a = [[n] for n in range(v.shape[0])]
Let me know if something is unclear, Thanks :)