Search code examples
pythonpandasdataframesortingmultiple-columns

Rearrange/mix column pandas


i have table like this:

ID Type I/P Value
ID1 Primary I 8
ID2 Primary I 3
ID3 Secondary P 6
ID4 Secondary I 2
ID5 Primary P 3
ID6 Primary I 4

I re order it this way:

ID Type I/P Value
ID1 Primary I 8
ID6 Primary I 4
ID2 Primary I 3
ID5 Primary P 3
ID3 Secondary P 6
ID4 Secondary I 2

But i was wondering if there is a way to rearrange/alternate the P/I values, something like this: (alternate between I/P but keep the type primary first, and get the bigger value per P/I)

ID Type I/P Value
ID1 Primary I 8
ID5 Primary P 3
ID6 Primary I 4
ID5 Primary P 3
ID3 Secondary P 6
ID4 Secondary I 2

Solution

  • here is one way to do it

    Note: your starting DF has two 'P' in the DF, the expected output has three 'P'. seems to be a typo

    
    # create a temp seq based on type and i/p
    # count for 'I' and 'P' both starts from 0
    # sort the result with type and seq
    
    
    out=df.assign(seq=df.groupby(['Type','I/P']).cumcount()).sort_values(['Type','seq','I/P']).drop(columns='seq')
    
    out
    
    ID  Type    I/P     Value
    0   ID1     Primary     I   8
    4   ID5     Primary     P   3
    1   ID2     Primary     I   3
    5   ID6     Primary     I   4
    3   ID4     Secondary   I   2
    2   ID3     Secondary   P   6