我在pandas数据框中有一个名为feature的列。我想按字母顺序排列“功能”的值。例如,在下表中,如何在订单年龄,颜色,性别中获得年龄下的所有值?
ID Feature
1001 color,age,gender
1002 age,gender,color
1003 age,color,gender
1004 color,age,gender
1005 age,color,gender
1006 color,age,gender
1007 age,color,gender
答案 0 :(得分:3)
这是一种方式,但老实说,我不喜欢它:
In [24]: df.Feature = df.Feature.str.split(',', expand=True).apply(lambda x: pd.Series(np.sort(x)).str.cat(sep=','), axis=1)
In [25]: df
Out[25]:
ID Feature
0 1001 age,color,gender
1 1002 age,color,gender
2 1003 age,color,gender
3 1004 age,color,gender
4 1005 age,color,gender
5 1006 age,color,gender
6 1007 age,color,gender