1

I have seen similar questions, but nothing that really matchs my problem. If I have a table of values such as:

value
a
b
b
c

I want to use pandas to add in columns to the table to show for example:

value a b
a 1 0 
b 0 1
c 0 0

I have tried the following:

df['a'] = 0

def string_count(indicator):
    if indicator == 'a':
        df['a'] == 1

df['a'].apply(string_count)

But this produces:

 0    None
 1    None
 2    None
 3    None

I would like to at least get to the point where the choices are hardcoded in (i.e I already know that a,b and c appear), but would even better if I could look set the column of strings and then insert a column for each unique string.

Am I approaching this the wrong way?

1 Answer 1

1
dummies = pd.get_dummies(df.value)

   a  b  c
0  1  0  0
1  0  1  0
2  0  1  0
3  0  0  1

If you only want to display unique occurrences, you can add:

dummies.index = df.value
dummies.drop_duplicates()

       a  b  c
value         
a      1  0  0
b      0  1  0
c      0  0  1

Alternatively:

df = df.join(pd.get_dummies(df.value))

  value  a  b  c
0     a  1  0  0
1     b  0  1  0
2     b  0  1  0
3     c  0  0  1

Where you could again .drop_duplicates() to only see unique entries from the value column.

Sign up to request clarification or add additional context in comments.

1 Comment

How can I alter this so the columns appear with the main dataframe and not as a separate dataframe?

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.