擅长:python、mysql、java
<p>您可以使用<code>groupby()</code>和<a href="https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.core.groupby.DataFrameGroupBy.aggregate.html" rel="nofollow noreferrer">^{<cd2>}</a>(也称<code>aggregate()</code>)在单个命令中获取第一个和最后一个值:</p>
<pre><code>df.groupby('id').agg({'lat': ['first', 'last'], 'lon': ['first', 'last']})
</code></pre>
<p>这给了你:</p>
<pre><code> lat lon
first last first last
id
1 41.144540 41.163172 -8.562926 -8.583821
2 41.163233 41.163198 -8.583838 -8.583886
</code></pre>
<p>这几乎正是您需要输入到<code>vincenty()</code>来计算每个<code>id</code>的距离的内容</p>