Home > Software engineering >  How do I sort multiple columns in a dataframe based on 2 columns, while keeping one of them static i
How do I sort multiple columns in a dataframe based on 2 columns, while keeping one of them static i

Time:11-04

My dataframe-

dfmodtestes
Account Key Name MonthSinceInception False
1       a0  Gu    20                 0.5     
1       a6  Gu    15                 0.4
1       a9  Gu    35                 0.9   
2       89  Pa    70                 0.8
2       01  Ra    08                 0.1

My objective is to keep the account order the same, but based on monthsinceinception all other column orders must be sorted by decending goal -

dfmodtestes
Account Key Name MonthSinceInception False
1       a9  Gu    35                 0.9     
1       a6  Gu    20                 0.5
1       a0  Gu    15                 0.4   
2       89  Pa    70                 0.8
2       01  Ra    08                 0.1

So as you can observe, Account order is the same but all other variables should change based on MonthsinceInception sort by descending

I attempted

dfmodwhtestes = dfmodwhtestes.sort_values(by = 'MonthsSinceInception', ascending = False)

But this just sorted the whole dataframe by descending based on month sinceinception, so what happend is account 2(month since inception =70) was in the first row then account 1 was in second row because month since inception is 35.

CodePudding user response:

Try using sort_values, note per docs ascending can accept a liset of bool:

df.sort_values(['Account', 'MonthSinceInception'], ascending=[True, False])

Output:

   Account Key Name  MonthSinceInception  False
2        1  a9   Gu                   35    0.9
0        1  a0   Gu                   20    0.5
1        1  a6   Gu                   15    0.4
3        2  89   Pa                   70    0.8
4        2  01   Ra                    8    0.1

CodePudding user response:

use a groupby over Accounts, and set sort=False:

pd.concat(
    dd
    for _, dd in df.sort_values(
        by=["MonthSinceInception"], ascending=False
    ).groupby(
        "Account", sort=False
    )
)

setup demo:

import pandas as pd
columns="Account Key Name MonthSinceInception False".split()
data =[
    [part for part in line.split(" ") if part] for line in """\
2       89  Pa    70                 0.8
2       01  Ra    08                 0.1
1       a0  Gu    20                 0.5     
1       a6  Gu    15                 0.4
1       a9  Gu    35                 0.9   
""".splitlines()]
data
df = pd.DataFrame(data, columns=columns)

which produces:

    Account     Key     Name    MonthSinceInception     False
0   2   89  Pa  70  0.8
1   2   01  Ra  08  0.1
4   1   a9  Gu  35  0.9
2   1   a0  Gu  20  0.5
3   1   a6  Gu  15  0.4

Note: I've intentionally swapped the account 1 and 2 to show how the groupby preserves order when specified.

  • Related