在数据帧python的每一行中按字母顺序对单词进行排序

use*_*990 2 python sorting string dataframe pandas

我在dataframe中有一列包含字符串值,如下所示:

sortdf=pd.DataFrame(data= {'col1':["hello are you","what happenend","hello you there","issue is in our program","whatt is your name"]})
Run Code Online (Sandbox Code Playgroud)

我想按字母顺序对元素中的每个单词进行排序.

期望的输出:

    col1
0    are hello you
1   happenend what 
2   hello there you 
3    is in issue  our program
4   is name whatt your
Run Code Online (Sandbox Code Playgroud)

我尝试使用以下代码执行此操作:

sortdf['col1']. sort()
Run Code Online (Sandbox Code Playgroud)

但是这段代码不起作用.

jpp*_*jpp 5

使用pd.Series.apply匿名lambda函数:

sortdf['col1'] = sortdf['col1'].apply(lambda x: ' '.join(sorted(x.split())))
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

或者,列表理解可能更有效:

sortdf['col1'] = [' '.join(sorted(x)) for x in sortdf['col1'].str.split()]
Run Code Online (Sandbox Code Playgroud)