Create And Maintain System State Based On Action

孤者浪人 提交于 2020-05-17 06:45:27

问题


I have a data set like this. The field set is meant to represent the user's currently in the system:

time    user     action     set
----------------------------------
1:00    A         walk      NaN
2:00    B         run       NaN
3:00    C         sit       NaN
4:00    D         enter     NaN
5:00    E         jump      NaN
6:00    X         leave     NaN
...   

I in order to achieve this, I need to: 1) Figure out the starting state 2) Any time a user has action=="enter", add them to the state 3) Any time a user has action=="leave", remove them from the state

**(a user can enter and leave multiple times)

Right now, I am achieving #1 by creating a pivot table of min time for action=="enter" and action=="leave". If the latter is greater than the former (leaves before entering) I can assume that they are in the original state. So far this is working.

Expected output here (I used above logic to determine the starting set based on rows not shown here) would be:

time    user     action     set
----------------------------------
1:00    A         walk      {A,B,C}
2:00    B         run       {A,B,C}
3:00    C         sit       {A,B,C}
4:00    D         enter     {A,B,C,D}
5:00    B         jump      {A,B,C,D}
6:00    A         leave     {B,C,D}

So essentially I want to make changes to set based on the previous set. I have been trying out combinations of shift() and df.set.apply, but either get errors or it doesn't respect the sequential order. How can this be achieved?

来源:https://stackoverflow.com/questions/61666084/create-and-maintain-system-state-based-on-action

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!