Home > database >  Reverse the order with the maximum value of each row
Reverse the order with the maximum value of each row

Time:06-09

I'm trying to ponder some table values ​​by reversing the order of the cells in python

Source Table:

https://i.stack.imgur.com/CXcUq.png

I need to reverse the order with the maximum value of each row:

line a:

    5 turns 1
    4 turns 2
    3 stays 3 *(for being the intermediary value)*
    2 turns 4
    1 turns 5

Result:

https://i.stack.imgur.com/UPED3.png

Is there any function that I can do this?

Dataframe: list_df = {'Order#1' : [5,8,np.nan], 'Order#2' : [4,6,1], 'Order#3' : [2,2,3], 'Order#4' : [1,1,2], 'Order#5' : [3,7,np.nan], 'Order#6' : [np.nan, 5, np.nan], 'Order#7' : [np.nan, 4, np.nan], 'Order#8' : [np.nan, 3, np.nan]}

df = pd.DataFrame(data=list_df)

CodePudding user response:

If you have consecutive values, you can subtract from the max and add the min per row:

out = df.rsub(df.max(1), axis=0).add(df.min(1), axis=0)

output:

   Order#1  Order#2  Order#3  Order#4  Order#5  Order#6  Order#7  Order#8
0      1.0      2.0      4.0      5.0      3.0      NaN      NaN      NaN
1      1.0      3.0      7.0      8.0      2.0      4.0      5.0      6.0
2      NaN      3.0      1.0      2.0      NaN      NaN      NaN      NaN
  • Related