I have the following dataset where I am trying to sort the row value in each column, but have not been able to find an efficient way of performing the operation. I was hoping someone would be able to point me in a more optimized way of sorting:
|Column_to_Sort|Desired_Output|
| a, x, z,c | a, c, x, z |
| ball, apple | apple, ball |
Essentially, I am trying to rearrange the list of items in the Column_to_sort alphabetically and separated by a comma.
I wrote the following code to perform the operation, however I don't believe it is the best way of performing the operation:
def sort_val(x):
String_ = x.split(",")
String_.sort()
return (String_)
df['Desired_Output'] = df['Column_to_Sort'].apply(lambda x: sort_val(x))