How to filter a pandas dataframe based on the length of a entry
<p>In a pandas dataframe I have a field 'amp' that should be populated by a list of length 495.  Is there a panda-ic way to quickly filter on this length, such that all rows with field 'amp' are not equal to 495 are dropped?</p>

<p>I tried</p>

<pre><code>df[len(df['amp']) == 495]
</code></pre>

<p>and this returned</p>

<pre><code>KeyError: False
</code></pre>

<p>Thanks in advance.</p>

<p>If you specifically need <code>len</code>, then @MaxU's answer is best.</p>

<p>For a more general solution, you can use the <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.map.html" rel="nofollow">map</a> method of a Series.</p>

<pre><code>df[df['amp'].map(len) == 495]
</code></pre>

<p>This will apply <code>len</code> to each element, which is what you want. With this method, you can use any arbitrary function, not just <code>len</code>.</p>

python pandas dataframe