Mi.
Mi.

Reputation: 510

Creating new columns with value names of other columns in pandas

I have a DataFrame as shown below.

DF =

id  w  R
1   A  L    
2   B  J      
3   C  L,J

I now want to create a new column that shows if the value in the column Rappears in the row.

DF2 =

id  w  R    L  J 
1   A  L    1  0 
2   B  J    0  1 
3   C  L,J  1  1 

I tried this line, but the result wasn't what I wanted:

for x in DF.R.unique():
  DF[x]=(DF.R==x).astype(int)


DF2 =

id  w  R    L  J L,J
1   A  L    1  0  0
2   B  J    0  1  0
3   C  L,J  0  0  1

What is needed to fix this? The DF is also very big and slow methods won't work.

Upvotes: 2

Views: 36

Answers (2)

BENY
BENY

Reputation: 323386

You need to specific the sep , in your example is ,

df.R.str.get_dummies(sep=',')
Out[192]: 
   J  L
0  0  1
1  1  0
2  1  1

Upvotes: 4

PMende
PMende

Reputation: 5470

I would use pandas' built-in str methods:

chars_to_count = ['L', 'J']
for char in chars_to_count:
    DF[char] = DF['R'].str.count(char)

Upvotes: 1

Related Questions