Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (18.4k points)

I have the Dataframe column with the following category:

    data = {'People': ['John','Mary','Andy','April'], 

             'Class': ['Math, Science','English, Math, Science','Math, Science','Science, English, Math']}

    

    df = pd.DataFrame(data, columns = ['People', 'Class'])

How can I create the new columns and transform the Dataframe as shown below:

> | People | Math | Science | English |

> ------------------------------------- 

> | John   | Math | Science |         | 

> | Mary   | Math | Science | English | 

> | Andy   | Math | Science |         |

> | April  | Math | Science | English |

1 Answer

0 votes
by (36.8k points)

The below code may help you:

columns = set([x for lst in df['Class'] for x in lst.replace(" ", "").split(",") ])

for col in columns:

  df[col] = ""*len(df)

for i, val in enumerate(df["Class"]):

  cl = val.replace(" ", "").split(",")

  print(cl)

  for value in cl:

    df.loc[i][value] = value

df.drop('Class', axis=1, inplace=True)

Output:

    People  Science English Math

0   John    Science         Math

1   Mary    Science English Math

2   Andy    Science         Math

3   April   Science English Math

If you want to know more about the Data Science then do check out the following Data Science which will help you in understanding Data Science from scratch

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...