作者:佩刚坤斌冠如_567 | 来源:互联网 | 2022-12-10 07:31
我在dataframe中有一列包含字符串值,如下所示:
sortdf=pd.DataFrame(data= {'col1':["hello are you","what happenend","hello you there","issue is in our program","whatt is your name"]})
我想按字母顺序对元素中的每个单词进行排序.
期望的输出:
col1
0 are hello you
1 happenend what
2 hello there you
3 is in issue our program
4 is name whatt your
我尝试使用以下代码执行此操作:
sortdf['col1']. sort()
但是这段代码不起作用.
1> jpp..:
使用pd.Series.apply
匿名lambda
函数:
sortdf['col1'] = sortdf['col1'].apply(lambda x: ' '.join(sorted(x.split())))
pd.Series.sort
是不合适的,因为(a)这对系列元素中的系列元素而不是单词进行排序,以及(b)该方法已被弃用而有利于sort_values
.
我们的想法是将字符串拆分为单词列表,按字母顺序排序,然后重新加入字符串.
结果:
col1
0 are hello you
1 happenend what
2 hello there you
3 in is issue our program
4 is name whatt your
或者,列表理解可能更有效:
sortdf['col1'] = [' '.join(sorted(x)) for x in sortdf['col1'].str.split()]