[英]How to condense rows in Pandas by removing everything between two conditions
I have a keyboard log that is telling me when keys are being press/released:我有一个键盘日志,它告诉我何时按下/释放键:
key state time
z 1 0.133
d 1 0.298
d 0 0.36
a 1 0.522
a 1 0.6455
a 1 0.7744
a 1 0.9033
a 1 1.0322
a 1 1.1611
a 1 1.29
a 1 1.4189
a 1 1.5478
a 1 1.6767
a 1 1.8056
a 1 1.9345
a 1 2.0634
z 0 2.1923
a 0 2.3212
When a key is pressed (state == 1), it continues to write that key until it returns to an up state (state = 0).当按下一个键(状态 == 1)时,它会继续写入该键,直到它返回到一个向上的 state(状态 = 0)。 How would I condense such a table so that it only includes rows where the key is first pressed and when it was let go?
我将如何压缩这样的表格,使其仅包含第一次按下键的行以及何时让 go? This form would make it easier to calculate the keypress duration.
这种形式可以更容易地计算按键持续时间。
key state time
z 1 0.133
d 1 0.298
d 0 0.36
a 1 0.522
z 0 2.1923
a 0 2.3212
My first thought is to use what I know, ie an ugly loop that would repeat for each key:我的第一个想法是使用我所知道的,即每个键都会重复的丑陋循环:
(1) Detect first instance of keypress and add row to new dataframe, (2) Go through rows until we see that key has been released, then add that to dataframe, (3) Append everything into one dataframe and then sort by time (1) Detect first instance of keypress and add row to new dataframe, (2) Go through rows until we see that key has been released, then add that to dataframe, (3) Append everything into one dataframe and then sort by time
I'm new to Pandas, but I know there must be a better way that properly takes advantage of the dataframe.我是 Pandas 的新手,但我知道必须有更好的方法来正确利用 dataframe。 I've discoevered dataframe.shift(), but can't quite wrap my head around how to deal with the non-constant distance in rows between the key presses/releases.
我发现了 dataframe.shift(),但我无法完全理解如何处理按键/释放之间的非恒定距离。
Any suggestions would be appreciated:)任何建议,将不胜感激:)
It's a simple application of first()
这是
first()
的简单应用
dfu = df.groupby(["key","state"], as_index=False).first().sort_values("time")
key![]() |
state ![]() |
time![]() |
|
---|---|---|---|
5 ![]() |
z ![]() |
1 ![]() |
0.133 ![]() |
3 ![]() |
d ![]() |
1 ![]() |
0.298 ![]() |
2 ![]() |
d ![]() |
0 ![]() |
0.36 ![]() |
1 ![]() |
a![]() |
1 ![]() |
0.522 ![]() |
4 ![]() |
z ![]() |
0 ![]() |
2.1923 ![]() |
0 ![]() |
a![]() |
0 ![]() |
2.3212 ![]() |
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.