How to read specific time for specific value row by row using python Here I have a dataset with date,time and one input column. This input column is included with values 3 and 4.
Here I want to read the datetime value which is having 4 row by row. Other rows which is having 3 values time should be skipped. I wrote the code but it didn't work properly.

data['date_time']= pd.to_datetime(data['date'] + " " + data['time'],
                    format='%d/%m/%Y %H:%M:%S', dayfirst=True)

x = data['X'].astype(int)
time = []
while x.all()==4:
   time=data['date_time']
   break
   time+=1
print(time)  


What I am expected output :



date          time   X            expected output
10/3/2018   6:15:00     3            skip row
10/3/2018   6:45:00     3            skip row
10/3/2018   7:45:00     3            skip row       
10/3/2018   9:00:00     4           read time 09:00:00
10/3/2018   9:25:00     3            skip row
10/3/2018   9:30:00     3            skip row
10/3/2018   11:00:00    4           read time 11:00:00      
10/3/2018   11:30:00    3            skip row 
10/3/2018   13:30:00    3            skip row
10/3/2018   13:50:00    3            skip row
10/3/2018   15:00:00    4           read time 15:00:00    




subest of my csv:



date           time   X
10/3/2018   6:15:00     3
10/3/2018   6:45:00     3
10/3/2018   7:45:00     3
10/3/2018   9:00:00     4
10/3/2018   9:25:00     3
10/3/2018   9:30:00     3
10/3/2018   11:00:00    4
10/3/2018   11:30:00    3
10/3/2018   13:30:00    3
10/3/2018   13:50:00    3
10/3/2018   15:00:00    4
10/3/2018   15:25:00    3
10/3/2018   16:25:00    3
10/3/2018   18:00:00    3
10/3/2018   19:00:00    3
10/3/2018   19:30:00    3
10/3/2018   20:00:00    4
10/3/2018   22:05:00    3
10/3/2018   22:15:00    3
10/3/2018   23:40:00    3
10/4/2018   6:58:00     3
10/4/2018   13:00:00    3
10/4/2018   16:00:00    4
10/4/2018   17:00:00    3
10/4/2018   18:00:00    3




My csv :
CSV FILE
One-liner:

result = data.loc[data.x == 4, "time"]    # assuming you expect just the time as output




Explanation

To describe your problem, first in English (then code):


  You want the rows of the data frame, where the value of the X column is 4, and all columns should be included in the result.


To check where coilumn X is 4, we can do this:

data.X == 4


This returns True or False for every single row. Now we can use this as a filter, by passing it to the pandas DataFrame's .loc accessor (loc = "locate").

result = data.loc[data.X == 4, :]
# Notation: dataframe[rows_we_want, columns_we_want]


Because we want all columns, we have left the colon : in there after the comma. Pandas assumes you want all columns by default, so we can actually leave it out to get the one-liner given above.

So if you just want to time to be given as output, you can filter for that by putting the name of the column after the comma:

result = data.loc[data.X == 4, "time"]