pandas Dataframe columns doing algorithm
<p>I have a dataframe like this:</p>

<pre><code>df = pd.DataFrame({
    'A': ['a', 'a', 'a', 'a', 'a'],
    'lon1': [128.0, 135.0, 125.0, 123.0, 136.0],
    'lon2': [128.0, 135.0, 139.0, 142.0, 121.0],
    'lat1': [38.0, 32.0, 38.0, 38.0, 38.0],
    'lat2': [31.0, 32.0, 35.0, 38.0, 29.0],
    'angle': [0, 0, 0, 0, 0]
})
</code></pre>

<p>I want to count the angle of each row by this function and save back to the angle column</p>

<pre><code>def angle(lon1,lat1,lon2,lat2):
    dx = lon2 - lon1
    dy = lat2 - lat1
    direction = 0;
    if ((dx == 0) &amp; (dy == 0)):  # same position
    return direction
    if (dx &gt; 0.0) :    
        direction = 90-np.arctan2(dy,dx)*180/np.pi
    elif (dy &gt; 0.0 ) :    
        direction = 180+(270-(np.arctan2(dy,dx)*180/np.pi))
    else :   
        direction = 360-(270+(np.arctan2(dy,dx)*180/np.pi))
        if (direction &lt; 0) :
            direction += 360

    return (direction.astype(int) % 360)  
</code></pre>

<p>I tried </p>

<pre><code>df.ix[df['A'].notnull(), 'angle'] =angle(
    df[df['A'].notnull()]['lon1'],
    df[df['A'].notnull()]['lat1'],
    df[df['A'].notnull()]['lon2'], 
    df[df['A'].notnull()]['lat2']) 
</code></pre>

<p>and I got an error</p>

<blockquote>
  <p>ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().</p>
</blockquote>

<p>I tried <code>for index,row in df.iterrows():</code> the result of for loop is ok but it took terribly long long time(original data is about 10 million rows ) </p>

<p>could anyone kindly give some efficient methods?</p>

<p>It seems like you are trying to apply function <code>angle(...)</code> to every row of your dataframe.</p>

<p>First it is necessary to cast all your string-typed numbers into float so as to calculate.</p>

<pre><code>df1.loc[:, "lon1"] = df1.loc[:, "lon1"].astype("float")
df1.loc[:, "lon2"] = df1.loc[:, "lon2"].astype("float")
df1.loc[:, "lat1"] = df1.loc[:, "lat2"].astype("float")
df1.loc[:, "lat2"] = df1.loc[:, "lat2"].astype("float")
</code></pre>

<p>There you go.</p>

<pre><code>df1.loc[:, "angle"] = df1.apply(lambda x: angle(x["lon1"], x["lat1"], x["lon2"], x["lat2"]), axis = 1)
</code></pre>

<p>As for performance concern, here are some tips for you.</p>

<ol>
<li>Profiling. </li>
<li>Use <code>numba</code> for JIT compilation and automatic vectorization of your function.</li>
</ol>

python pandas