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>Try this:</p>

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

<p>Demo:</p>

<pre><code>In [77]: df
Out[77]:
                 a
0  [1, 2, 3, 4, 5]
1        [1, 2, 3]
2             [-1]

In [78]: df.a.str.len()
Out[78]:
0    5
1    3
2    1
Name: a, dtype: int64

In [79]: df[df.a.str.len() == 3]
Out[79]:
                 a
1        [1, 2, 3]
</code></pre>

python pandas dataframe