1
business_id               categories
1K4qrnfyzKzGgJPBEcJaNQ   ['Tiki Bars', 'Nightlife', 'Mexican', 'Restaurants', 'Bars']
dTWfATVrBfKj7Vdn0qWVWg   ['Restaurants', 'Chinese', 'Food Court']

This is how my data frame looks like. The second column has a list as each value.

I am currently using this code but it doesn't work and gives me an error below:

categorical_data = pd.DataFrame()
for i in range(len(data)):
  for j in range(len(data.iloc[i][1])):
    categorical_data = pd.concat(data.iloc[i][0], data.iloc[i][1][j])

TypeError: first argument must be an iterable of pandas objects, you passed an object of type "str"

Is there a way where I can add the list of categories to the same restaurant? I've tried using the 'get_dummies' but it's not able to split the lists and encode them.

jpp
  • 159,742
  • 34
  • 281
  • 339
Danny
  • 470
  • 4
  • 14

1 Answers1

1

I believe you need MultiLabelBinarizer with join:

from sklearn.preprocessing import MultiLabelBinarizer

mlb = MultiLabelBinarizer()
df1 = pd.DataFrame(mlb.fit_transform(df['categories']),columns=mlb.classes_, index=df.index)
df2 = df[['business_id']].join(df1)
print (df2)
              business_id  Bars  Chinese  Food Court  Mexican  Nightlife  \
0  1K4qrnfyzKzGgJPBEcJaNQ     1        0           0        1          1   
1  dTWfATVrBfKj7Vdn0qWVWg     0        1           1        0          0   

   Restaurants  Tiki Bars  
0            1          1  
1            1          0  

Or create index from first column:

df1 = pd.DataFrame(mlb.fit_transform(df['categories']),
                   columns=mlb.classes_, index=df['business_id'])
print (df1)
                        Bars  Chinese  Food Court  Mexican  Nightlife  \
business_id                                                             
1K4qrnfyzKzGgJPBEcJaNQ     1        0           0        1          1   
dTWfATVrBfKj7Vdn0qWVWg     0        1           1        0          0   

                        Restaurants  Tiki Bars  
business_id                                     
1K4qrnfyzKzGgJPBEcJaNQ            1          1  
dTWfATVrBfKj7Vdn0qWVWg            1          0  
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252