按字符串

时间:2018-05-17 15:06:23

标签: python list pandas sorting series

我想根据字符串的长度(最长的第一个)对数据框中的列表进行排序

brand       serie
&tradition  [&tradition flowerpot, &tradition spinning pen, &tradition test]
Dove        [dove deeply nourishing, dove men+care]
Adidas      [adidas ace, adidas gel]
Vasaline    [vaseline advanced repair, vaseline men]
Boska       [boska dutch, boska test2, boska testing]

我希望结果看起来像这样:

brand       serie
.....       ......
Boska       [boska testing, boska dutch, boska test2]

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

您可以将pd.Series.applysorted

一起使用
def sorter(x):
    return sorted(x, key=len, reverse=True)

df['serie'] = df['serie'].apply(sorter)