1
$\begingroup$

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

$\endgroup$

3 Answers 3

1
$\begingroup$

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"]
$\endgroup$
2
  • $\begingroup$ Thank you for the great explanation. But sorry to say when I applied your code it gave me this error " 'Series' objects are mutable, thus they cannot be hashed" $\endgroup$
    – bala
    Sep 18, 2019 at 6:21
  • $\begingroup$ Where exactly did you get that error? It tells you which line in the error message. I just made a small change to my final line, maybe that fixes it for you? $\endgroup$
    – n1k31t4
    Sep 18, 2019 at 6:23
0
$\begingroup$

You can use where conditions of dataframe data = data[data['X'] == 4]

$\endgroup$
2
  • $\begingroup$ Thank you for the response $\endgroup$
    – bala
    Sep 18, 2019 at 10:08
  • $\begingroup$ You got the required output? $\endgroup$ Sep 18, 2019 at 11:04
0
$\begingroup$

try the dataframe assign. it create a new column where X==4 filter the time is applied

  dataDict={'date': {0: '10/3/2018', 1: '10/3/2018', 2: '10/3/2018', 3: '10/3/2018', 4: '10/3/2018', 5: '10/3/2018', 6: '10/3/2018', 7: '10/3/2018', 8: '10/3/2018', 9: '10/3/2018', 10: '10/3/2018', 11: '10/3/2018', 12: '10/3/2018', 13: '10/3/2018', 14: '10/3/2018', 15: '10/3/2018', 16: '10/3/2018', 17: '10/3/2018', 18: '10/3/2018', 19: '10/3/2018', 20: '10/4/2018', 21: '10/4/2018', 22: '10/4/2018', 23: '10/4/2018', 24: '10/4/2018'}, 'time': {0: '6:15:00', 1: '6:45:00', 2: '7:45:00', 3: '9:00:00', 4: '9:25:00', 5: '9:30:00', 6: '11:00:00', 7: '11:30:00', 8: '13:30:00', 9: '13:50:00', 10: '15:00:00', 11: '15:25:00', 12: '16:25:00', 13: '18:00:00', 14: '19:00:00', 15: '19:30:00', 16: '20:00:00', 17: '22:05:00', 18: '22:15:00', 19: '23:40:00', 20: '6:58:00', 21: '13:00:00', 22: '16:00:00', 23: '17:00:00', 24: '18:00:00'}, 'X': {0: 3, 1: 3, 2: 3, 3: 4, 4: 3, 5: 3, 6: 4, 7: 3, 8: 3, 9: 3, 10: 4, 11: 3, 12: 3, 13: 3, 14: 3, 15: 3, 16: 4, 17: 3, 18: 3, 19: 3, 20: 3, 21: 3, 22: 4, 23: 3, 24: 3}}

df=pd.DataFrame(dataDict)
df['X']=df['X'].astype(int)
df=df.assign(find_4=lambda x: x[x['X']==4]['time']).fillna('')
print(df)

output:
    date      time  X    find_4
0   10/3/2018   6:15:00  3          
1   10/3/2018   6:45:00  3          
2   10/3/2018   7:45:00  3          
3   10/3/2018   9:00:00  4   9:00:00
4   10/3/2018   9:25:00  3          
5   10/3/2018   9:30:00  3          
6   10/3/2018  11:00:00  4  11:00:00
7   10/3/2018  11:30:00  3          
8   10/3/2018  13:30:00  3          
9   10/3/2018  13:50:00  3          
10  10/3/2018  15:00:00  4  15:00:00
11  10/3/2018  15:25:00  3          
12  10/3/2018  16:25:00  3          
13  10/3/2018  18:00:00  3          
14  10/3/2018  19:00:00  3          
15  10/3/2018  19:30:00  3          
16  10/3/2018  20:00:00  4  20:00:00
17  10/3/2018  22:05:00  3          
18  10/3/2018  22:15:00  3          
19  10/3/2018  23:40:00  3          
20  10/4/2018   6:58:00  3          
21  10/4/2018  13:00:00  3          
22  10/4/2018  16:00:00  4  16:00:00
23  10/4/2018  17:00:00  3          
24  10/4/2018  18:00:00  3
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.