0

Here is my dataframe:

Name   Job
A      Back-end Engineer
B      Front-end Engineer;Product Manager
C      Product Manager;Business Development;System Analyst

I want to transform that dataframe into dummy (one hot encoding) like this:

Name   Back-end Engineer   Business Development   Front-end Engineer   Product Manager  System Analyst
A      1                   0                      0                    0                0
B      0                   0                      1                    1                0
C      0                   1                      0                    1                0

I tried to use pandas.get_dummies but it's failure because the variable is multivariate.

OctavianWR
  • 217
  • 1
  • 16
  • Check answer [here](https://stackoverflow.com/questions/52389290/reshaping-and-encoding-multi-column-categorical-variables-to-one-hot-encoding), this question already been asked. – Benales May 13 '19 at 09:27
  • Thanks @Benales . So it means that i need to split my column into some variables – OctavianWR May 13 '19 at 09:32

1 Answers1

1

You can try something like this:

import pandas as pd
from collections import defaultdict


df = pd.read_csv("path/to/your.csv")

jobs = df["Job"]
job_list = set()
for job in jobs:
  job_names = job.split(";")
  for job_name in job_names:
    job_list.add(job_name)

new_df = defaultdict(list)
for index, row in df.iterrows():
  new_df["Name"].append(row["Name"])
  for job in job_list:
    if job in row["Job"]:
      new_df[job].append(1)
    else:
      new_df[job].append(0)

new_df = pd.DataFrame.from_dict(new_df)
new_df.to_csv("/path/to/new.csv")
Kıvanç Yüksel
  • 701
  • 7
  • 17