Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Select the dataframe based on the certain time value in pandas

I have a dataframe

df = pd.DataFrame([["A","11:40 AM"],["B","12:51 PM"],["C","6:33 PM"],["D","11:13 AM"],["E","7:13 PM"]],columns=["id","time"])
id    time
A   11:40 AM
B   12:51 PM
C    6:33 PM
D   11:13 AM
E    7:13 PM

I want to select only those rows which are < 6:30 PM.

Expected output:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

df = pd.DataFrame([["A","11:40 AM"],["B","12:51 PM"],["D","11:13 AM"]],columns=["id","time"])
id    time
A   11:40 AM
B   12:51 PM
D   11:13 AM

I tried df[(df['time'].dt.time < '18:30:00')]. It is giving an error. How to do it?

>Solution :

Try converting to datetime first:

df.loc[pd.to_datetime(df['time']).le(pd.Timestamp('18:30:00'))]

Output:

  id      time
0  A  11:40 AM
1  B  12:51 PM
3  D  11:13 AM
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading