Sancta Ignis Sancta Ignis - 2 years ago 125
Python Question

Python CSV: Grab all values in row with conditions for time values

example for csv data with condition that I'm trying to get:

c1,c2,v1,v2,p1,p2,r1,a1,f1,f2,f3,Time_Stamp

0,2.3,0.6,-0.9,-0.5,1,-1,941.0,0,50,0,13/06/2017 16:38:00
0,2.3,0.6,-0.9,-0.5,1,-1,941.0,0,50,0,13/06/2017 16:38:01
0,2.3,0.6,-0.9,-0.5,1,-1,941.0,0,50,0,13/06/2017 16:38:02
0,2.3,0.6,-0.9,-0.5,1,-1,941.0,0,50,0,13/06/2017 16:38:03
0,2.3,0.6,-0.9,-0.5,1,-1,941.0,0,50,0,13/06/2017 16:38:04
0,2.3,0.6,-0.9,-0.5,1,-1,941.0,0,50,0,13/06/2017 16:38:05
0,2.3,0.6,-0.9,-0.5,1,-1,941.0,0,50,0,13/06/2017 16:38:06
0,2.3,0.6,-0.9,-0.5,1,-1,941.0,0,50,0,13/06/2017 16:38:07
0,2.3,0.6,-0.9,-0.5,1,-1,941.0,0,50,0,13/06/2017 16:38:08
0,2.3,0.6,-0.9,-0.5,1,-1,941.0,0,50,0,13/06/2017 16:38:09
0,2.3,0.6,-0.9,-0.5,1,-1,941.0,0,50,0,13/06/2017 16:38:10
0,2.3,0.6,-0.9,-0.5,1,-1,941.0,0,50,0,13/06/2017 16:38:11
0,2.3,0.6,-0.9,-0.5,1,-1,941.0,0,50,0,13/06/2017 16:38:12
0,2.3,0.6,-0.9,-0.5,1,-1,941.0,0,50,0,13/06/2017 16:38:13
0,2.3,0.6,-0.9,-0.5,1,-1,941.0,0,50,0,13/06/2017 16:38:14
0,2.3,0.6,-0.9,-0.5,1,-1,941.0,0,50,0,13/06/2017 16:38:15
415.7,12.5,30.2,154.6,4675.2,1,-1,5199.4,0,50,0,13/06/2017 16:38:16
0,2.3,0.6,-0.9,-0.5,1,-1,941.0,0,50,0,13/06/2017 16:38:17
0,2.3,0.6,-0.9,-0.5,1,-1,941.0,0,50,0,13/06/2017 16:38:18
0,2.3,0.6,-0.9,-0.5,1,-1,941.0,0,50,0,13/06/2017 16:38:19
0,2.3,0.6,-0.9,-0.5,1,-1,941.0,0,50,0,13/06/2017 16:38:20
0,2.3,0.6,-0.9,-0.5,1,-1,941.0,0,50,0,13/06/2017 16:38:21


I have this output after running the code below, specifically at the *bottom..
What I'm trying to get is
[for e.g.] Condition: if the row where
Time
value is
16:38:15
, and the following row's
Time
value is
16:38:17
( where the next row's
Time
value skipped 1 second )...
Print both rows ( with
Time
16:38:15
and the row with
Time
16:38:17
)

13/06/2017 16:38:00
13/06/2017 16:38:01
13/06/2017 16:38:02
13/06/2017 16:38:03
13/06/2017 16:38:04
13/06/2017 16:38:05
13/06/2017 16:38:06
13/06/2017 16:38:07
13/06/2017 16:38:08
13/06/2017 16:38:09
13/06/2017 16:38:10
13/06/2017 16:38:11
13/06/2017 16:38:12
13/06/2017 16:38:13
13/06/2017 16:38:14
13/06/2017 16:38:15
13/06/2017 16:38:17
13/06/2017 16:38:18
13/06/2017 16:38:19
13/06/2017 16:38:20
13/06/2017 16:38:21


Codes for reading the csv:

import plotly
import plotly.plotly as py
import plotly.graph_objs as go
import plotly.figure_factory as FF
import numpy as np
from datetime import date,time,datetime
import pandas as pd
%matplotlib inline
import matplotlib.pyplot as plt

def readcsv(x): #def function to read csv files based on code below*
Data = pd.read_csv(x, parse_dates=['Time_Stamp'], infer_datetime_format=True)
Data['Date'] = Data.Time_Stamp.dt.date #date column in DataFrame
Data['Time'] = Data.Time_Stamp.dt.time #time column in DataFrame

Data['Time_Stamp'] = pd.to_datetime(Data['Time_Stamp'])
print(Data[1:6])
return Data

Data = readcsv('datafile.csv')#*

def getMask(start,end,Data):
mask = (Data['Time_Stamp'] > start) & (Data['Time_Stamp'] <= end)
return mask;

start = '2017-06-13 16:00:00'
end = '2017-06-13 16:40:00'
sel_timerange = Data.loc[getMask(start, end, Data)]
#timeR.plot(x='Time_Stamp', y='AC_Input_Current', style='-', color='black')


Part of the Code that runs to receive the above Data( at the top ):

timeCondition = (timeR.loc[timeR['c1'] <= 5.0])
print(timeCondition)

with open('welding_data_by_selRange.csv','a', newline='') as duraweld:
a = csv.writer(duraweld)
data = [countIC2 ,countIC, Datetime]
a.writerow(data)

Answer Source

you can easily compare two consecutive times:

for i in range(df.shape[0] - 1):
    row1 = df.iloc[i]
    row2 = df.iloc[i+1]
    if (row2[-1] - row1[-1]).seconds > 1:
        print (row1.values)
        print(row2.values)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download