piRSquared
piRSquared

Reputation: 294488

Sort columns by their single character names with vowels first

Consider the dataframe 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 rearrange the columns such that vowels come before consonants and alphabetically otherwise.

I can sort the columns alphabetically with 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 do I do this dynamically considering I don't know the exact letters? I do know that they are single character ascii capital letters.

Upvotes: 4

Views: 123

Answers (1)

cs95
cs95

Reputation: 402852

You'll need sorted + reindex.

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

sorted will sort on multiple predicates if you pass it a list/container of tuples generated with zip.

Alternatively, adopting piR's suggestion and using a lambda to sort:

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

Upvotes: 6

Related Questions