0

I am trying to convert the following dataframe:

cat  userid  val
1    1       10
1    2       15
1    3       22
2    1       33
2    2       54
2    3       44

to the following format: [[10, 15, 22], [33, 54, 44]]

This is my code:

df.groupby(['cat', 'userid'])['val'].apply(list)

And, this is what I get:

cat          userid
1            1          [10]
             2          [15]
             3          [22]
2            1          [33]
             2          [54]
             3          [44]

Any suggestions?

renakre
  • 8,001
  • 5
  • 46
  • 99
  • 1
    use `df.groupby('cat')['val'].apply(list).tolist()` – jezrael Sep 30 '17 at 13:23
  • @Zero sorry it was a mistake. corrected. – renakre Sep 30 '17 at 13:24
  • Duplicate of [grouping rows in list in pandas groupby](https://stackoverflow.com/questions/22219004/grouping-rows-in-list-in-pandas-groupby) and [how extract a vector from groupby pandas in python](https://stackoverflow.com/questions/43210427/how-extract-a-vector-from-groupby-pandas-in-python) and [Grouping in Pandas](https://stackoverflow.com/questions/31901506/grouping-in-pandas) – Zero Sep 30 '17 at 13:28
  • Ya, 2.nd and 3.rd is OK, first not. So deleting answer. – jezrael Sep 30 '17 at 13:32
  • Ya, it is sad ;) I am working on it. – jezrael Sep 30 '17 at 13:33

0 Answers0