I have a mystery error on line 13 of this program. I'm trying to select all the times between 22.00 and 23.00. I've tried with and without datetime, with and without apostrophes, but I get the same error every time: "SyntaxError: invalid syntax"
The csv file is 8 columns and 'complaint_time' is in the format 12:22:01 AM.
What am I missing...?
Thanks :)
\\\
## Imports
import pandas as pd
from datetime import datetime, timedelta
df = pd.read_csv("nyc_noise_complaints.csv")
df['complaint_time'] = pd.to_datetime(df['complaint_time'], format="%H:%M.%S")
##Define a dictionary
Dict2 = {'BRONX': None, 'BROOKLYN': None, 'QUEENS': None, 'MANHATTAN': None, 'STATEN ISLAND': None}
##Define a function
def function2(arg):
late = df.loc[(df['borough']==arg) & (df['complaint_time']<= 23:00.0.to_datetime() & >=22:00.0.to_datetime())]
number = late.count()[0]
## Update the dictionary
Dict2 [arg]=number
## Loop through the list and run the function
for key in Dict2:
function2(key)
smallest2 = min(Dict2, key=Dict2.get)
print("Smallest number of complaints between 2200 & 2300: " str(smallest2) ' ' str(Dict2[smallest2]))
///
CodePudding user response:
try this:
late = df.loc[(df['borough']==arg) & ((df['complaint_time'].dt.hour<= 23) & (df['complaint_time'].dt.hour >=22))]
#or
late=df[(df['borough']==arg) & ((df['complaint_time'].dt.hour <= 23) & (df['complaint_time'].dt.hour >=22))]