Home > Mobile >  Pandas split dataframe by sessions
Pandas split dataframe by sessions

Time:12-09

I've got the next DataFrame:

id sec
1 45
2 1
3 176
1 19
1 876
3 123

I want to split it to groups by id by sessions, or create multiple dataframes of this sessions. Like I want to have sessions of each id (session is When more than 30 seconds have passed between user actions)

For example: sessions for id 1: [45, 19], [876]

I tried gruopby and cat, but I have no idea how to implement this

CodePudding user response:

To identify the session you can use:

df['session'] = (df.sort_values(by=['id', 'sec'])
                   .groupby('id')['sec']
                   .apply(lambda s: s.diff().gt(30).cumsum().add(1))
                )

Output:

   id  sec  session
0   1   45        1
1   2    1        1
2   3  176        2
3   1   19        1
4   1  876        2
5   3  123        1
  • Related