python - grouping rows in list in pandas groupby -
i have pandas data frame like:
a 1 2 b 5 b 5 b 4 c 6
i want group first column , second column lists in rows:
a [1,2] b [5,5,4] c [6]
is possible using pandas groupby?
you can using groupby
group on column of interest , apply
list
every group:
in [1]: # create dataframe df = pd.dataframe( {'a':['a','a','b','b','b','c'], 'b':[1,2,5,5,4,6]}) df out[1]: b 0 1 1 2 2 b 5 3 b 5 4 b 4 5 c 6 [6 rows x 2 columns] in [76]: df.groupby('a')['b'].apply(list) out[76]: a [1, 2] b [5, 5, 4] c [6] name: b, dtype: object
Comments
Post a Comment