Python Pandas条件无法准确识别行

时间:2018-03-12 04:40:57

标签: python pandas jupyter-notebook jupyter data-science

这是jupyter笔记本的输入和输出。我需要帮助来确定我无法准确选择和设置' went_out'中数据的原因。列。

enter image description here

红色下划线单元格应该显示来自其自己行的日期时间列的数据,但只有一个正确显示它。事实证明,许多符合我条件的行没有被选中和设置。

enter image description here

这是我使用的代码示例:



# your answer here
df.loc[(df['reading_type'] == 'motion') & (df['value'] == 255), 'event'] = 'motion on'
df.loc[(df['reading_type'] == 'motion') & (df['value'] == 0), 'event'] = 'motion off'

df2 = df.loc[(df['reading_type'] == 'door') | (df['event'] == 'motion on')].copy()
df2.loc[(df['event'] == 'door close') & (df['event'].shift(-1) == 'door open'), 'went_out'] = df2['datetime']
df2




以下是jupyter笔记本文件和csv文件的链接:

  1. Jupyter笔记本: https://drive.google.com/file/d/15f6NQrM4UoAZlzRhK35TOKyhPJnmWWdU/view?usp=sharing

  2. CSV文件: https://drive.google.com/file/d/1hZudSVbT91ESj2qkzrJ--CbVdrzVCmce/view?usp=sharing

1 个答案:

答案 0 :(得分:1)

据我所知,你正试图写出门关闭时的日期和时间。这可能是您想要的解决方案的一部分。而不是寻找开门然后关闭的条件,你可以只使用门关闭条件索引' went_out'柱。

df.loc[(df['reading_type'] == 'door') & (df['value'] == 255), 'event'] = 'door on'
df.loc[(df['reading_type'] == 'door') & (df['value'] == 0), 'event'] = 'door off'

df2 = df[df['reading_type'] == 'door'].copy()
# The line below is modified
df2.loc[df2['event'] == 'door off', 'went_out'] = df2[df2['event'] == 'door off']['datetime']
print(df2)

输出如下:

    id  datetime    device  location    reading_type    value   event   went_out
284 284 2018-01-01 07:57:56 Door    door    door    255.0   door on NaN
285 285 2018-01-01 07:58:12 Door    door    door    0.0 door off    2018-01-01 07:58:12
294 294 2018-01-01 08:29:25 Door    door    door    255.0   door on NaN
295 295 2018-01-01 08:29:38 Door    door    door    0.0 door off    2018-01-01 08:29:38
357 357 2018-01-01 09:16:38 Door    door    door    255.0   door on NaN
361 361 2018-01-01 09:17:40 Door    door    door    0.0 door off    2018-01-01 09:17:40

希望这有用。

修改
在门打开后关门时获取日期和时间的条件

df2.loc[((df2['event'].shift(-1) == 'door on') & (df2['event']=='door off') ), 'went_out'] = df2[df2['event']=='door off']['datetime']

print(df2[df2['event'] == 'door off'])

    id  datetime    device  location    reading_type    value   event   went_out
285 285 2018-01-01 07:58:12 Door    door    door    0.0 door off    2018-01-01 07:58:12
295 295 2018-01-01 08:29:38 Door    door    door    0.0 door off    NaN
361 361 2018-01-01 09:17:40 Door    door    door    0.0 door off    2018-01-01 09:17:40
509 509 2018-01-01 15:50:46 Door    door    door    0.0 door off    2018-01-01 15:50:46

如果这可以解决您的问题,请告诉我。