python - Flattening shallow list with pandas -
i trying flatten content of column of pandas.dataframe
contains list of list cannot find proper way correct output.
instead of different question asked in stackoverflow same subject, here focus flattering process inside each row of pandas.dataframe
.
here toy example :
df = pd.dataframe({ 'recipe': [['olive oil', 'low sodium chicken broth', 'cilantro leaves', 'chile powder', 'fresh thyme'], ['coconut milk', 'frozen banana', 'pure acai puree', 'almond butter'], ['egg', 'whole milk', 'extra-virgin olive oil', 'garlic cloves', 'corn kernels', 'chicken breasts']], 'category': ['a', 'b', 'b'] }) df_grouped = df.groupby('category')['recipe'].apply(lambda x: x.tolist()) df_grouped = df_grouped.reset_index() df_grouped['recipe'][1]
this produce following output :
[['coconut milk', 'frozen banana', 'pure acai puree', 'almond butter'], ['egg', 'whole milk', 'extra-virgin olive oil', 'garlic cloves', 'corn kernels', 'chicken breasts']]
my objective merge row row every list of words or sentences. tried following code split every letter.
join = lambda list_of_lists: (val sublist in list_of_lists val in sublist) df_grouped['merged'] = df_grouped['recipe'].apply(lambda x: list(join(x))) df_grouped['merged']
this produce :
0 [o, l, i, v, e, , o, i, l, l, o, w, , s, o, ... 1 [c, o, c, o, n, u, t, , m, i, l, k, f, r, o, ...
i following output each row, 1 array words
['coconut milk', 'frozen banana', 'pure acai puree', 'almond butter', 'egg', 'whole milk', 'extra-virgin olive oil', 'garlic cloves', 'corn kernels', 'chicken breasts']
just change join :
join = lambda list_of_lists: (val sublist in list_of_lists val in sublist if isinstance(sublist, list))
here output :
in[69]: df_grouped['merged'] = df_grouped['recipe'].apply(lambda x: list(join(x))) in[70]: df_grouped['merged'] out[70]: 0 [olive oil, low sodium chicken broth, cilantro... 1 [coconut milk, frozen banana, pure acai puree,... name: merged, dtype: object
Comments
Post a Comment