希望将
image.png
转变成
image.png
data.ingredients.apply(pd.Series) \
.merge(data, right_index = True, left_index = True) \
.drop(["ingredients"], axis = 1) \
.melt(id_vars = ['cuisine', 'id'], value_name = "ingredient") \
.drop("variable", axis = 1) \
.dropna()
有些版本的panda melt 命令有些变动
需要使用 pd.melt
每一步的参考见:
https://www.mikulskibartosz.name/how-to-split-a-list-inside-a-dataframe-cell-into-rows-in-pandas/
里边有每一步的变动, 变化,
https://chrisalbon.com/python/data_wrangling/pandas_expand_cells_containing_lists/
网友评论