Question: This is probably a very basic question but I haven't been able to find the answer so here goes...

Question:

Is there an say way to sort the values alphabetically while also removing any duplicate instances?

Here's what I have:

```
data = ['Car | Book | Apple','','Book | Car | Apple | Apple']
df = pd.DataFrame(data,columns=['Labels']
print(df)

    Labels
0   Car | Book | Apple
1   
2   Book | Car | Apple | Apple

```

Desired Output:

```
    Labels
0   Apple | Book | Car
1   
2   Apple | Book | Car

```

Thanks!

Answer: 
`str.join` after `str.split`

```
df=df.replace({' ':''},regex=True)
df.Labels.str.split('|').apply(set).str.join('|')
Out[339]: 
0    Apple|Book|Car
1                  
2    Apple|Book|Car
Name: Labels, dtype: object

```

Base on the comment adding `sorted`

```
df.Labels.str.split('|').apply(lambda x : sorted(set(x),reverse=False)).str.join(' | ')

```
`df['Labels'].str.split('|')` will split the string on `|` and return a list

```
#0             [Car ,  Book ,  Apple]
#1                                 []
#2    [Book ,  Car ,  Apple ,  Apple]
#Name: Labels, dtype: object

```

See that there are extra spaces in the resulting list elements. One way to remove those is by applying `str.strip()` to each element in the list:

```
df['Labels'].str.split('|').apply(lambda x: map(str.strip, x))
#0           [Car, Book, Apple]
#1                           []
#2    [Book, Car, Apple, Apple]
#Name: Labels, dtype: object

```

Finally we apply the `set` constructor to remove duplicates, sort the values, and join them back together using `" | "` as a separator:

```
df['Labels'] = df['Labels'].str.split('|').apply(
    lambda x: " | ".join(sorted(set(map(str.strip, x))))
)
print(df)
#               Labels
#0  Apple | Book | Car
#1                    
#2  Apple | Book | Car

```