0

I have a dataframe that contains information about cuisines and their respective ingredients. The ingredients are stored in a column with type of list of strings, ['ingredients'], as shown in the image below:

enter image description here

I tried to one hot encode each ingredient so I used the answer in this post for reference.

However, I got an error message shown below:

code:

train_w_stemming_df = pd.DataFrame(mlb.fit_transform(train_w_stemming_df['ingredients']),
         columns=mlb.classes_,
         index = train_w_stemming_df.index)

error message:

ValueError                                Traceback (most recent call last)
/usr/local/lib/python3.7/dist-packages/pandas/core/internals/managers.py in create_block_manager_from_blocks(blocks, axes)
   1670                 blocks = [
-> 1671                     make_block(values=blocks[0], placement=slice(0, len(axes[0])))
   1672                 ]

6 frames
ValueError: Wrong number of items passed 1, placement implies 43

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
/usr/local/lib/python3.7/dist-packages/pandas/core/internals/managers.py in create_block_manager_from_blocks(blocks, axes)
   1679         blocks = [getattr(b, "values", b) for b in blocks]
   1680         tot_items = sum(b.shape[0] for b in blocks)
-> 1681         raise construction_error(tot_items, blocks[0].shape[1:], axes, e)
   1682 
   1683 

ValueError: Shape of passed values is (29774, 1), indices imply (29774, 43)

enter image description here

How can I fix this error?

desertnaut
  • 57,590
  • 26
  • 140
  • 166
Abe164
  • 1
  • 3
  • 1
    Please copy-paste the error traceback as text (and format it as code): that is far more readable (and searchable) than a picture. – 9769953 Dec 25 '21 at 12:38
  • Please add the relevant section of code that produces the error as well. – 9769953 Dec 25 '21 at 12:39
  • 1
    Note that you're creating a new dataframe, with just one Series (the encoded ingredients), while still specifying all classes. That won't fit. – 9769953 Dec 25 '21 at 12:44

0 Answers0