Siavash
Siavash

Reputation: 190

Sorting letters within each cell in pandas

I have this data frame:

AB  DC
BA  WZ
DC  ZW

And I want to sort letters of each cell using pandas, like this:

AB CD
AB WZ
CD WZ

Thanks!

Upvotes: 4

Views: 158

Answers (2)

BENY
BENY

Reputation: 323226

Try

df = df.applymap(lambda x : ''.join(sorted(list(x))))
  col1 col2
0   AB   CD
1   AB   WZ
2   CD   WZ

Upvotes: 6

ansev
ansev

Reputation: 30920

Use:

df.applymap(lambda x:  ''.join(sorted(x)))

Upvotes: 2

Related Questions