Home > Blockchain >  Graph Sensor Data with Python and Matplotlib
Graph Sensor Data with Python and Matplotlib

Time:02-14

I have a csv file like this:

sensor_name, time
bed, 01:08:40
bed, 01:09:33
bed, 01:09:51
bed, 01:11:06
kitchen, 01:27:57
kitchen, 01:30:03
living, 01:32:51
living, 01:33:06
living, 01:34:06
living, 01:35:06
living, 01:37:06
bed, 01:40:50
bed, 01:41:06
bed, 01:42:54
bed, 01:45:06
living, 01:50:30

In order to track movements, I just want to plot - with matplotlib - only the values where a sensor change occurs, thus the first and last before the change and eliminate the rest. The expected result looks like this:

sensor_name, time
bed, 01:08:40
bed, 01:11:06
kitchen, 01:27:57
kitchen, 01:30:03
living, 01:32:51
living, 01:37:06
bed, 01:40:50
bed, 01:45:06
living, 01:50:30

Thanks, all the best.

CodePudding user response:

You can compute a mask by checking if the sensor_name changes (get the next row with shift):

mask = df['sensor_name'].ne(df['sensor_name'].shift())

# change   previous row
df[mask|mask.shift(-1)]

output:

   sensor_name      time
0          bed  01:08:40
3          bed  01:11:06
4      kitchen  01:27:57
5      kitchen  01:30:03
6       living  01:32:51
10      living  01:37:06
11         bed  01:40:50
14         bed  01:45:06
15      living  01:50:30
  • Related