Andrés Bustamante
Andrés Bustamante

Reputation: 462

group dataframe using unique values of column as index

What is the most efficient way of group or stack the different values of dataframe indexing by value?

this df:

color   alert

blue    A
blue    B
red     A
green   C

to:

color   alert

blue    [A, B]
red     A
green   C

Upvotes: 0

Views: 40

Answers (1)

yatu
yatu

Reputation: 88236

You need a groupby and to aggregate into a list:

df.groupby('color').alert.agg(list).reset_index()

   color   alert
0   blue  [A, B]
1  green     [C]
2    red     [A]

Upvotes: 5

Related Questions