Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 560
Siavash Avatar asked Jan 25 '23 17:01

Siavash


2 Answers

Try

df = df.applymap(lambda x : ''.join(sorted(list(x))))
  col1 col2
0   AB   CD
1   AB   WZ
2   CD   WZ
like image 59
BENY Avatar answered Feb 13 '23 02:02

BENY


Use:

df.applymap(lambda x:  ''.join(sorted(x)))
like image 33
ansev Avatar answered Feb 13 '23 01:02

ansev