Mamed
Mamed

Reputation: 772

Merge values of columns in a cell based on unique values of another column

From this:

  Ordinal   Timestamp                   id_easy     lat/long    
    1       2016-06-01T08:18:46.000Z    22          (44.9484, 7.7728)   
    2       2016-06-01T08:28:05.000Z    22          (44.9503, 7.7748)   
    3       2016-06-01T08:28:09.000Z    22          (44.9503, 7.7748)       
    1       2016-06-01T06:31:05.000Z    16          (45.0314, 7.6181)

Based on unique values of id_easy and also by illustrating time range when it happened to this:

 Timestamp                   id_easy     lat/long    
 08:18:46-08:28:09           22          (44.9484, 7.7728),(44.9503, 7.7748),(44.9503, 7.7748)

Upvotes: 0

Views: 35

Answers (1)

vlemaistre
vlemaistre

Reputation: 3331

You should use the groupby() function. Here is what it could look like :

df.groupby('id_easy').agg({'lat/long' : list, 'Timestamp':min})

Output :

  id_easy                                           lat/long  \
0      16                                 [(45.0314,7.6181)]  
1      22  [(44.9484,7.7728), (44.9503,7.7748), (44.9503,... 
  Timestamp  
0  2016-06-01T06:31:05.000Z  
1  2016-06-01T08:18:46.000Z 

Upvotes: 1

Related Questions