1

I would like to create dummies based on column values...

This is what the df looks like

I want to create this

This is so far my approach

import pandas as pd
df =pd.read_csv('test.csv')
v =df.Values

v_set=set()
for line in v:
line=line.split(',')
for x in line:
    if x!="":
        v_set.add(x)
    else:
        continue

   for val in v_set:
    df[val]=''

By the above code I am able to create columns in my df like this

How do I go about updating the row values to create dummies? This is where I am having problems.

Thanks in advance.

June7
  • 19,874
  • 8
  • 24
  • 34
Krish Kk
  • 21
  • 6
  • Possible duplicate of [Pandas convert a column of list to dummies](https://stackoverflow.com/questions/29034928/pandas-convert-a-column-of-list-to-dummies) – Brad Solomon Jan 18 '18 at 03:05
  • 1
    If you have a fairly large Series, consider the answer from [Paulo Alves](https://stackoverflow.com/a/41676643/7954504) in the link above – Brad Solomon Jan 18 '18 at 03:06

1 Answers1

1

You could use pandas.Series.str.get_dummies. This will alllow you to split the column directly with a delimiter.

df = pd.concat([df.ID, df.Values.str.get_dummies(sep=",")], axis=1)

    ID  1   2   3   4
0   1   1   1   0   0
1   2   0   0   1   1

df.Values.str.get_dummies(sep=",") will generate

    1   2   3   4
0   1   1   0   0
1   0   0   1   1

Then, we do a pd.concat to glue to df together.

Tai
  • 7,684
  • 3
  • 29
  • 49