Search code examples
pythonpandassensors

Filtering static/stationary areas


I was trying to filter my sensor data. My objective is to filter the sensor data where the data is more or less stationary over a period of time. can anyone help me in this

time : 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20  

sensor : 121
115
122
123
116
117
113
116
113
114
115
112
116
129
123
125
130
120
121
122

this is a sample data, i need to take the first data and compare it to the next 20 seconds of data, if all the 20 datas is in the the range of +or- 10 then i need to filter these 20 datas to another column, and i need to continue this process of filtering


Solution

  • However your question is not very clear but from my understanding what you want is between time duration of 20 seconds if the sensor is in between the range of +10 and -10 from the first reading then you have to append those values to new column and above or below that should not be considered. I tried replicating your DataFrame and you could go ahead in this way:

    import pandas as pd
    data = {'time':[1, 2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23], 
            'sensor':[121, 115, 122, 123,116,117,113,116,113,114,115,112,116,129,123,125,130,120,121,122,123,124,144]}
    
    df_new = pd.DataFrame(data) #I am taking time duration of 23 seconds where 23rd second data is out of range as 144 - 121 > 10
    
        time  sensor
    0      1     121
    1      2     115
    2      3     122
    3      4     123
    4      5     116
    5      6     117
    6      7     113
    7      8     116
    8      9     113
    9     10     114
    10    11     115
    11    12     112
    12    13     116
    13    14     129
    14    15     123
    15    16     125
    16    17     130
    17    18     120
    18    19     121
    19    20     122
    20    21     123
    21    22     124
    22    23     144
    
    list = []
    for i in range(0, len(df_new['sensor'])):
        if 0 <= df_new['time'][i] - df_new['time'][0] <= 23: #you take here 20 which is your requirement instead of 23 as I am doing to demonstrate for the value of 144
            if -10 < df_new['sensor'][0] - df_new['sensor'][i] < 10:
                list.append(df_new['sensor'][i])
            else:
                list.append('out of range')
        else:
            break
    
    df_new['result'] = list
    
    df_new
    
        time  sensor          result
    0      1     121           121
    1      2     115           115
    2      3     122           122
    3      4     123           123
    4      5     116           116
    5      6     117           117
    6      7     113           113
    7      8     116           116
    8      9     113           113
    9     10     114           114
    10    11     115           115
    11    12     112           112
    12    13     116           116
    13    14     129           129
    14    15     123           123
    15    16     125           125
    16    17     130           130
    17    18     120           120
    18    19     121           121
    19    20     122           122
    20    21     123           123
    21    22     124           124
    22    23     144  out of range