This can be done in 2 steps, generate a new column that creates the expanded str values, then groupbyinto 'A' and into this new column:apply list
In [62]:
df['expand'] = df.apply(lambda x: ','.join([x['B']] * x['quantity']), axis=1)
df.groupby('A')['expand'].apply(list)
Out[62]:
A
1 [foo, baz,baz, bar,bar, faz]
2 [foo,foo, bar]
3 [foo,foo,foo]
Name: expand, dtype: object
EDIT
OK after the inspiration from @Jianxun Li's answer:
In [130]:
df.groupby('A').apply(lambda x: np.repeat(x['B'].values, x['quantity']).tolist())
Out[130]:
A
1 [foo, baz, baz, bar, bar, faz]
2 [foo, foo, bar]
3 [foo, foo, foo]
dtype: object
It also works:
In [131]:
df.groupby('A').apply(lambda x: list(np.repeat(x['B'].values, x['quantity'])))
Out[131]:
A
1 [foo, baz, baz, bar, bar, faz]
2 [foo, foo, bar]
3 [foo, foo, foo]
dtype: object