Skip to content Skip to sidebar Skip to footer

Pandas Sort List Returned By Str.split()

Given a Pandas Series of type str, I want to sort the result returned by str.split. For example, given the Series s = pd.Series(['abc,def,ghi','ghi,abc']) I would like to get s2 =

Solution 1:

try this:

In [70]: s.str.split(',').map(lambda x: ','.join(sorted(x)))
Out[70]:
0    abc,def,ghi
1        abc,ghi
dtype: object

Solution 2:

You can use the apply function which is very useful in Pandas.

s.apply(lambda x: ','.join(sorted(x.split(','))))

0    abc,def,ghi
1        abc,ghi

Post a Comment for "Pandas Sort List Returned By Str.split()"