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 :)
JavaScript
x
33
33
1
\
2
3
## Imports
4
5
import pandas as pd
6
from datetime import datetime, timedelta
7
df = pd.read_csv("nyc_noise_complaints.csv")
8
df['complaint_time'] = pd.to_datetime(df['complaint_time'], format="%H:%M.%S")
9
10
##Define a dictionary
11
Dict2 = {'BRONX': None, 'BROOKLYN': None, 'QUEENS': None, 'MANHATTAN': None, 'STATEN ISLAND': None}
12
13
##Define a function
14
15
def function2(arg):
16
17
late = df.loc[(df['borough']==arg) & (df['complaint_time']<= 23:00.0.to_datetime() & >=22:00.0.to_datetime())]
18
number = late.count()[0]
19
20
## Update the dictionary
21
22
Dict2 [arg]=number
23
24
## Loop through the list and run the function
25
26
for key in Dict2:
27
function2(key)
28
29
smallest2 = min(Dict2, key=Dict2.get)
30
print("Smallest number of complaints between 2200 & 2300: "+str(smallest2)+' '+str(Dict2[smallest2]))
31
32
///
33
Advertisement
Answer
try this:
JavaScript
1
7
1
late = df.loc[(df['borough']==arg) & ((df['complaint_time'].dt.hour<= 23) & (df['complaint_time'].dt.hour >=22))]
2
3
#or
4
5
late=df[(df['borough']==arg) & ((df['complaint_time'].dt.hour <= 23) & (df['complaint_time'].dt.hour >=22))]
6
7