Alireza
Alireza

Reputation: 6848

How to convert list into set in pandas?

I have a dataframe as below:

           date                     uids
0  2018-11-23  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
1  2018-11-24  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]

When I use set to convert it to set it fails:

df['uids'] = set(df['uids'])  # IT FAILS!

How should I convert list into set in place?

Upvotes: 17

Views: 24540

Answers (2)

nogmos
nogmos

Reputation: 909

For anyone who wants to know the fastest way to convert list into set in Pandas:

Method 1:

df['uids'] = df.apply(lambda row: set(row['uids']), axis=1)

Method 2:

df['uids'] = df['uids'].apply(set)

Method 3:

df['uids'] = df['uids'].map(set)

I run timeit with repeat(50, 5) on DF with 4000 rows:

Method 1 - mean:  0.13299, min:  0.12723
Method 2 - mean:  0.01319, min:  0.01207
Method 3 - mean:  0.01261, min:  0.01164

Upvotes: 2

ilyakhov
ilyakhov

Reputation: 1319

You should use apply method of DataFrame API:

df['uids'] = df.apply(lambda row: set(row['uids']), axis=1)

or

df = df['uids'].apply(set) # great thanks to EdChum

You can find more information about apply method here.

Examples of use

df = pd.DataFrame({'A': [[1,2,3,4,5,1,1,1], [2,3,4,2,2,2,3,3]]})
df = df['A'].apply(set)

Output:

>>> df
0    set([1, 2, 3, 4, 5])
1          set([2, 3, 4])
Name: A, dtype: object

Or:

>>> df = pd.DataFrame({'A': [[1,2,3,4,5,1,1,1], [2,3,4,2,2,2,3,3]]})
>>> df['A'] = df.apply(lambda row: set(row['A']), axis=1)
>>> df
                      A
0  set([1, 2, 3, 4, 5])
1        set([2, 3, 4])

Upvotes: 18

Related Questions