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:
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)
How can I fix this error?