Home > Back-end >  how to make list of tuples of rows with same value in another row
how to make list of tuples of rows with same value in another row

Time:10-23

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)...]

CodePudding user response:

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)]
  • Related