1

I have a dataframe with more rows and columns but example with one is here:

id    values 
1   [v1, v2, v1]

How to get unique values from list in pandas column? Desired output in second column v1, v2 I have tried with df['values'].unique() but obviously it's not working.

3 Answers 3

2

A simple solution would be agg pd.unique i.e

df = pd.DataFrame({'x' : [['v','w','x','v','x']]})

df['x'].agg(pd.unique) # Also np.unique

0    [v, w, x]
Name: x, dtype: object

or

df['x'].agg(set).agg(list)

0    [v, w, x]
Name: x, dtype: object
Sign up to request clarification or add additional context in comments.

2 Comments

Try time testing:)
Apply is faster than agg, no argument with that here
2

Again

df['new']=list(map(set,df['values'].values))

Timing

%timeit df['values'].agg(np.unique)
The slowest run took 6.78 times longer than the fastest. This could mean that an intermediate result is being cached.
100 loops, best of 3: 6.99 ms per loop
%timeit list(map(set,df['values'].values))
The slowest run took 55.36 times longer than the fastest. This could mean that an intermediate result is being cached.
10000 loops, best of 3: 228 µs per loop
%timeit df['values'].apply(lambda x: list(set(x)))
1000 loops, best of 3: 743 µs per loop

1 Comment

map is good - very pythonic, I somehow always forget to use it in the context of dataframe. May be next time:)
1

Try

df['values'] = df['values'].apply(lambda x: list(set(x)))


    id  values
0   1   [v2, v1]

Note: values is a pandas attribute so its better to avoid using that as column name.

Time comparison:

df= pd.DataFrame({'id':[1]*1000,    'values' :[['v1', 'v2', 'v1']]*1000})
%timeit df['values'].agg(np.unique)

34.7 ms ± 2.01 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)


%timeit df['values'].apply(lambda x: list(set(x)))

1.98 ms ± 259 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

1 Comment

Glad you used np.unique, pd.unique is way slower than that

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.