Sort columns by their single character names first with vowels

Consider a data block df

df = pd.DataFrame(np.arange(25).reshape(5, 5), columns=list('CBESA'))
df

    C   B   E   S   A
0   0   1   2   3   4
1   5   6   7   8   9
2  10  11  12  13  14
3  15  16  17  18  19
4  20  21  22  23  24

I want to reorder the columns so that the vowels come before the consonants and in alphabetical order otherwise.

I can sort the columns alphabetically using sort_index

df.sort_index(1)

    A   B   C   E   S
0   4   1   0   2   3
1   9   6   5   7   8
2  14  11  10  12  13
3  19  16  15  17  18
4  24  21  20  22  23

But that leaves 'E'out of order.
I can get what I want "manually"

df[list('AEBCS')]

    A   E   B   C   S
0   4   2   1   0   3
1   9   7   6   5   8
2  14  12  11  10  13
3  19  17  16  15  18
4  24  22  21  20  23

How can I do this dynamically, given that I do not know the exact letters? I know that they are capital letters of one character.

+4
source share
1 answer

You will need sorted+ reindex.

df.reindex(columns=[
    x[1] for x in sorted(zip(~df.columns.isin(list('AEIOU')), df.columns))
])

sortedwill sort by several predicates if you pass it the list / container of tuples generated with zip.

, piR lambda :

df.reindex(
    columns=sorted(df.columns, key=lambda x: (x not in 'AEIOU', x))
)

    A   E   B   C   S
0   4   2   1   0   3
1   9   7   6   5   8
2  14  12  11  10  13
3  19  17  16  15  18
4  24  22  21  20  23
+6

Source: https://habr.com/ru/post/1695225/


All Articles