Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I do unique keyword simplification and sort alphabetically on pandas

Here's my dataset

id  keyword
1   transfer atm transfer atm
2   transfer transfer atm
3   atm transfer hospital

Here's what I want is sort keyword alphabetically and make this unique, based on alphabetical the word on keyword after sort alphabetically is atm, hospital, and transfer

id  keyword
1   atm transfer
2   atm transfer
3   atm hospital transfer
like image 633
Nabih Bawazir Avatar asked Dec 18 '22 18:12

Nabih Bawazir


1 Answers

Try this:

df['keyword']=df['keyword'].apply(lambda x:' '.join(sorted(set(x.split()))))

O/P:

   id                keyword
0   1           atm transfer
1   2           atm transfer
2   3  atm hospital transfer

Explanation:

  1. split the words by white space.
  2. find the common words i.e., remove repeated words.
  3. sort the selected words
like image 123
Mohamed Thasin ah Avatar answered Feb 15 '23 23:02

Mohamed Thasin ah