using len() in Pandas dataframe
<p>This is the look of my <code>dataframe</code>:</p>

<p><img src="http://i.stack.imgur.com/9C22C.png" alt="dataframe">
and I want to list a list of senators whose surname is more than 9 characters long</p>

<p>So I think the code should be like this:</p>

<pre><code>df[len(df.Surname) &gt;9 ]
</code></pre>

<p>but there will be a compile error, where did I go wrong?</p>

<p>You want that <code>len</code> function to work on strings but you passed a Series. It returns a single value that shows the length of that series. Therefore, <code>len(df['Surname']) &gt; 9</code> returns a True or False. In return, <code>df[len(df['Surname'] &gt; 9]</code> is evaluated as <code>df[True]</code> or <code>df[False]</code>.</p>

<p>Instead, you should do something like</p>

<pre><code>df[df['Surname'].str.len() &gt; 9]
</code></pre>

<p>Here, with str accessor, len method is applied to all elements in that Series.</p>

python pandas dataframe