Python: use a function in pandas lambda expression
<p>I have the following code, trying to find the hour of the 'Dates' column in a data frame:</p>

<pre><code>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)
</code></pre>

<p>where the <code>print(df['Dates'].head(3))</code> looks like:</p>

<pre><code>0    2015-05-13 23:53:00
1    2015-05-13 23:53:00
2    2015-05-13 23:33:00
</code></pre>

<p>However, I got the following error:</p>

<pre><code>    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')
</code></pre>

<p>Does anyone know what I missed? Thanks!</p>

<hr>

<p>Note that if I put the function directly in the lambda line like below, everything works fine:</p>

<pre><code>df['hour'] = df.apply(lambda x: x['Dates'][11:13], axis=1).astype(float)
</code></pre>