2

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.

1
  • Check str.split : df['Col1'].str.split(',') Commented Dec 11, 2019 at 10:03

1 Answer 1

5

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()]
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.