1

I have multiple columns with multiple values separated by ",".

eg: restaurant_type column has values like 'Casual Dining' as well as 'Cafe, Casual Dining'.

This is causing issues while visualizing due to the existence of 87 unique values.

I'mahdi
  • 23,382
  • 5
  • 22
  • 30
Aman Rao
  • 27
  • 4
  • 3
    can you add example for your dataframe – phœnix Dec 24 '22 at 19:03
  • From what I understood from your question, I believe that the explode() function can solve it. Maybe this answer will help you: [sttackoverflow-Split (explode) pandas dataframe string entry to separate rows](https://stackoverflow.com/questions/12680754/split-explode-pandas-dataframe-string-entry-to-separate-rows) – rzz Dec 24 '22 at 19:59

1 Answers1

0
https://www.pythonpool.com/python-code-to-convert-a-table-to-first-normal-form/


import pandas as pd
import numpy as np


df = pd.DataFrame({'Name': ['David', 'Glenn', 'Steve'], 'Subjects': [
    ['English', 'Math'], ['Math'], ['Science', 'English']]})

print(df)
lens = list(map(len, df['Subjects'].values))

res = pd.DataFrame({'Name': np.repeat(
    df['Name'], lens), 'Subject': np.concatenate(df['Subjects'].values)})

print(res)
BH10001
  • 52
  • 10