Home > Mobile >  Pandas dataframe: How to export a list of lists of values grouped by value in another column
Pandas dataframe: How to export a list of lists of values grouped by value in another column

Time:01-14

So given a df like:

    timestamp   x_1  y_1   x_2  y_2
0  1649010310     1    4     7    7
1  1649010310     7    8     0    1
2  1649010310     5    1     0    0
3  1649010311     2    0     1    6
4  1649010311     8    4     2    7

I'd like to export a list of the x_1, y_1, x_2, y_2 coordinates grouped by the timestamp

Is there a faster way than using a for loop through the df to export a list of lists like:

[[[1, 4, 7, 7],
  [7, 8, 0, 1],
  [5, 1, 0, 0]],
 [[2, 0, 1, 6],
  [8, 4, 2, 7]]]

?

Of course we can loop over the df using something like:

for i in range(len(df)):
    # create list of lists

But, I was wondering if there was something that was more efficient in pandas (or numpy?) that could make this process faster.

CodePudding user response:

Yes, you can use groupby and apply methods.

grouped = df.groupby('timestamp')
result = grouped[['x_1', 'y_1', 'x_2', 'y_2']].apply(lambda x: x.values.tolist())
result = result.tolist()
  • Related