I have the following code, trying to find the hour of the 'Dates' column in a data frame:
print(df['Dates'].head(3))
df['hour'] = df.apply(lambda x: find_hour(x['Dates']), axis=1)
def find_hour(self, input):
return input[11:13].astype(float)
where the print(df['Dates'].head(3)) looks like:
0 2015-05-13 23:53:00
1 2015-05-13 23:53:00
2 2015-05-13 23:33:00
However, I got the following error:
df['hour'] = df.apply(lambda x: find_hour(x['Dates']), axis=1)
NameError: ("global name 'find_hour' is not defined", u'occurred at index 0')
Does anyone know what I missed? Thanks!
Note that if I put the function directly in the lambda line like below, everything works fine:
df['hour'] = df.apply(lambda x: x['Dates'][11:13], axis=1).astype(float)