0

Image of code and dataframe

I am trying to make a list of tuples of the 'snippet_time_bounds' in one row for each corresponding 'filename'. So first row from example would have the first filename and then a list of tuples like:

[(0.0, 4.032),(4.032, 8.064),(8.064, 12.096),(12.096, 16.128),(20.16, 24.192)...]
user207421
  • 305,947
  • 44
  • 307
  • 483
  • Please include any relevant information [as text directly into your question](https://stackoverflow.com/editing-help), do not link or embed external images of source code or data. Images make it difficult to efficiently assist you as they cannot be copied and offer poor usability as they cannot be searched. See: [Why not upload images of code/errors when asking a question?](https://meta.stackoverflow.com/q/285551/15497888) – Henry Ecker Oct 23 '21 at 03:06
  • If you need assistance formatting a small sample of your DataFrame as a copyable piece of code for SO see [How to make good reproducible pandas examples](https://stackoverflow.com/q/20109391/15497888). – Henry Ecker Oct 23 '21 at 03:06

1 Answers1

1

Your groupby is slightly off, and you are looking for agg(list), I believe

By way of example:

df=pd.DataFrame({'filename':[1,2,3,1,2,2,3], 'tuples':[(1,2),(71,2),(10,2),(1,20),(11,222),(31,23),(18,22)]})

df.groupby(['filename']).agg(list)

                                  tuples
filename
1                      [(1, 2), (1, 20)]
2         [(71, 2), (11, 222), (31, 23)]
3                    [(10, 2), (18, 22)]
Jonathan Leon
  • 5,440
  • 2
  • 6
  • 14