2

I have a column in my dataframe that is a list with values from rows. Is there any way to get the same columns with unique values in these lists sorted also.

This is my dataframe column.

ListProds
['YIZ12FF-A', 'YIZ12FF-A', 'YIIE2FF-A', 'YIR72FF-A', 'YIR72FF-A', 'YIR72FF-A']
['HYY32ZY-A', 'HYY32ZY-A']
['YI742FF-A', 'YI742FF-A', 'YI742FF-A', 'YI742FF-A']
['YI762FF-A', 'YI762FF-A', 'YI762FF-A', 'YI762FF-A', 'YI762FF-A', 'YI762FF-A', 'YI6E2FF-A', 'YI6E2FF-A', 'YI6E2FF-A']
['YI762FF-A', 'YI762FF-A', 'YI762FF-A', 'YI742FF-A', 'YI742FF-A', 'YI742FF-A', 'YI772FF-A', 'YI772FF-A', 'YI772FF-A']
['YIIE2FF-A']
['YIZ12FF-A', 'YIZ12FF-A', 'YIZ12FF-A', 'YIZ12FF-A', 'YIRE2FF-A', 'YIRE2FF-A', 'YIRE2FF-A', 'YIRE2FF-A', 'YIIK2FF-A', 'YIIK2FF-A', 'YIZ32FF-A', 'YZ3F2FF-A']
['YIY32FF-A']
['YNF82FF-A']
['YTFL2FF-A', 'YTFL2FF-A', 'YTHR2FF-A', 'YU0Y2FF-A']
['YI6A2FF-A', 'YI6A2FF-A', 'YI6A2FF-A']
['YI772FF-A']
['YTZR2FF-A']
['YIRF2FF-A', 'YIRF2FF-A', 'YIRF2FF-A', 'YIRF2FF-A']
['YI752FF-A', 'YI752FF-A', 'YI752FF-A']

I tried a few methods used in Python without success.

What is the pandas method for removing duplicated values from list into a dataframe?

1
  • ListProds.apply(np.unique) Commented Nov 5, 2019 at 12:51

1 Answer 1

3

Convert values to sets and then sort them:

df['ListProds'] = df['ListProds'].apply(lambda x: sorted(set(x)))

Or like mentioned @Chris A in comments use np.unique:

df['ListProds'] = df['ListProds'].apply(lambda x: np.sort(np.unique(x)))
#if lists are sorted
#df['ListProds'] = df['ListProds'].apply(np.unique)

print (df)
                                            ListProds
0                   [YIIE2FF-A, YIR72FF-A, YIZ12FF-A]
1                                         [HYY32ZY-A]
2                                         [YI742FF-A]
3                              [YI6E2FF-A, YI762FF-A]
4                   [YI742FF-A, YI762FF-A, YI772FF-A]
5                                         [YIIE2FF-A]
6   [YIIK2FF-A, YIRE2FF-A, YIZ12FF-A, YIZ32FF-A, Y...
7                                         [YIY32FF-A]
8                                         [YNF82FF-A]
9                   [YTFL2FF-A, YTHR2FF-A, YU0Y2FF-A]
10                                        [YI6A2FF-A]
11                                        [YI772FF-A]
12                                        [YTZR2FF-A]
13                                        [YIRF2FF-A]
14                                        [YI752FF-A]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.