Daniel Martinez
Daniel Martinez

Reputation: 397

Create columns in dataframe based on csv field

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

Upvotes: 2

Views: 47

Answers (1)

BENY
BENY

Reputation: 323226

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

Upvotes: 4

Related Questions