Compare length of values of a column
<p>I have a data frame with 20 columns, two of the columns being Company1 and Company2. I want a resultant data frame with only those rows in which length of Company1 and Company2 don't differ by more than 5 characters. How do I accomplish this task using pandas? </p>

<p>You can use <code>.str.len()</code> to get access to the number of characters in the <code>Company</code> columns, then calculate the difference with vectorized subtraction of pandas series and create a logic vector with the condition for subsetting:</p>

<pre><code>df[abs(df.Company1.str.len() - df.Company2.str.len()) &lt;= 5]
</code></pre>

python pandas