Home > Software design >  For each column group of values order by list without dropping duplicates
For each column group of values order by list without dropping duplicates

Time:07-05

if i have a dataframe like this one:

level org
1 A
1 A
2 A
2 A
3 A
3 A
1 B
1 B
2 B
2 B
3 B
3 B
1 C
1 C
2 C
2 C
3 C
3 C

I want to use a list based on unique values of the level column:

levels=[1,2,3]

to order each individual org group and get an output like this preserving the duplicates:

level org
1 A
2 A
3 A
1 A
2 A
3 A
1 B
2 B
3 B
1 B
2 B
3 B
1 C
2 C
3 C
1 C
2 C
3 C

CodePudding user response:

Create new column with cumcount

df['new'] = df.groupby(['level','org']).cumcount()
out = df.sort_values(['org','new','level'])
Out[71]: 
    level org  new
0       1   A    0
2       2   A    0
4       3   A    0
1       1   A    1
3       2   A    1
5       3   A    1
6       1   B    0
8       2   B    0
10      3   B    0
7       1   B    1
9       2   B    1
11      3   B    1
12      1   C    0
14      2   C    0
16      3   C    0
13      1   C    1
15      2   C    1
17      3   C    1
  • Related