Dicing in python

2024/11/16 8:24:42

Code:-

df = pd.DataFrame({'col1':t, 'col2':wordList})
df.columns=['DNT','tweets']
df.DNT = pd.to_datetime(df.DNT, errors='coerce')
check=df[ (df.DNT < '09:20:00') & (df.DNT > '09:00:00') ]

Don't know why this code is not working. Does anyone know what is wrong in the above code?

Answer

You can compare with datetime format like this:

Suppose:

import pandas as pd
import datetime
df = pd.DataFrame({'col1':['2017-05-24 09:06:11','2017-05-24 09:06:12','2017-05-24 09:00:00'], 'col2':['hello','hi','bonjour']})
df.columns=['DNT','tweets']
df.DNT = pd.to_datetime(df.DNT, errors='coerce')
df

df will be:

    DNT                  tweets
0   2017-05-24 09:06:11  hello
1   2017-05-24 09:06:12  hi
2   2017-05-24 09:00:00  bonjour

Then you can compare with start and end:

end = datetime.datetime(2017, 5, 24, 9, 20) #2017-05-24 09:20:00
start = datetime.datetime(2017, 5, 24, 9) #2017-05-24 09:00:00
df[ (df.DNT < end) & (df.DNT > start) ]

Then filter result will be:

    DNT                  tweets
0   2017-05-24 09:06:11  hello
1   2017-05-24 09:06:12  hi
https://en.xdnf.cn/q/119213.html

Related Q&A

How to collect all specified hrefs?

In this test model I can collect the href value for the first (tr, class_=rowLive), Ive tried to create a loop to collect all the others href but it always gives IndentationError: expected an indented …

How do I sort data highest to lowest in Python from a text file?

I have tried multiple methods in doing this but none of them seem to work The answer comes up alphabetically insteadf=open("class2.txt", "r") scores=myfile.readlines() print(sorted(…

Determine if a variable is an instance of any class

How to determine if a variable is an instance in Python 3? I consider something to be an instance if it has __dict__ attribute.Example:is_instance_of_any_class(5) # should return False is_instance_of…

Method POST not allowed with Django Rest Framework

Im trying to create a JSON API compliant rest service using Django Rest Framework JSON API: https://django-rest-framework-json-api.readthedocs.io/en/stable/index.htmlI think Im stuck at the Django Rest…

Running multiple queries in mongo`

I have a collection and want to get a set of results that met a set of conditions. I understand the Mongo doesnt let you use joins so I would need to run separate queries and join the results into a si…

Error in Calculating neural network Test Accuracy

I tried to train my neural network, and then evaluate its testing accuracy. I am using the code at the bottom of this post to train. The fact is that for other neural networks, I can evaluate the testi…

Adding stats code to a function in Python

Im relatively new to Python and trying to learn how to write functions. The answer to this post highlights how to get certain stats from a dataframe and I would like to use it in a function.This is my…

Multiple Histograms, each for a label of x-axis, on the same graph matplotlib

I am trying to plot a graph to show different behaviors by males and females with regards to a certain activity, for different age-groups. So, if the age groups are: [1-10,11-20,21-30...] I would like …

How can I split a string in Python? [duplicate]

This question already has answers here:Closed 11 years ago.Possible Duplicates:Split python string every nth character?What is the most &ldquo;pythonic&rdquo; way to iterate over a list in chu…

How to enable media device access in Edge browser using Selenium?

I have a Selenium test I want to run in Edge against a page which uses the webcam and microphone. In order for those media devices to work in the browser, the user has to allow the site access to the d…