问题
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