Lilly
Lilly

Reputation: 988

convert a string delimited by comma into list in pandas

I have a dataframe which has two columns. Each column has comma separated string. I am trying to convert this string into list and hence I can subtract two lists as a_b = list(set(a) - set(b)) for each row for the following dataset.

  Col1      Col2
  a,b,c,f   d,f,g
  d,g       w,a,d
  f,g,h     f,g,h

I tried converting items into list as below

df1['Col1']tolist()

But it doesn't seem to work. Thanks.

Upvotes: 2

Views: 2458

Answers (1)

Chris Adams
Chris Adams

Reputation: 18647

You can use str.split to split a comma-separated string to list. You can also use apply(set) for your specific purposes IIUC:

(df['Col1'].str.split(',').apply(set) - df['Col2'].str.split(',').apply(set)).tolist()

[out]

[{'a', 'b', 'c'}, {'g'}, set()]

Upvotes: 5

Related Questions