2

I have a pandas dataframe with the column "Values" that has comma separated values:

Row|Values
1|1,2,3,8
2|1,4

I want to create columns based on the CSV, and assign a boolean indicating if the row has that value, as follows:

Row|1,2,3,4,8
1|true,true,true,false,true
2|true,false,false,true,false

How can I accomplish that?

Thanks in advance

1 Answer 1

4

Just using get_dummies, check the link here and the astype(bool) change 1 to True 0 to False

df.set_index('Row')['Values'].str.get_dummies(',').astype(bool)
Out[318]: 
        1      2      3      4      8
Row                                  
1    True   True   True  False   True
2    True  False  False   True  False
Sign up to request clarification or add additional context in comments.

1 Comment

@pygo Just the base usage for get_dummies function , I will add the link

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.