简体   繁体   English

使用列的唯一值作为索引对数据框进行分组

[英]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:这个 df:

color   alert

blue    A
blue    B
red     A
green   C

to:到:

color   alert

blue    [A, B]
red     A
green   C

You need a groupby and to aggregate into a list :您需要一个groupby并聚合到一个list

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM