擅长:python、mysql、java
<p>使用<a href="https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.set_index.html" rel="nofollow noreferrer">^{<cd2>}</a>、<a href="https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.groupby.html" rel="nofollow noreferrer">^{<cd3>}</a>和<a href="https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.transform.html" rel="nofollow noreferrer">^{<cd4>}</a><a href="https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.idxmax.html" rel="nofollow noreferrer">^{<cd5>}</a>创建助手<code>Series</code>。然后使用<a href="https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#boolean-indexing" rel="nofollow noreferrer">^{<cd6>}</a>:</p>
<pre><code># If neccessary cast to datetime dtype
# df['time'] = pd.to_datetime(df['time'])
s = df.set_index('type').groupby('id')['time'].transform('idxmax')
df[df.type == s.values]
</code></pre>
<p>[输出]</p>
<pre><code> time id type
1 2013-11-02 1 xF1yz
4 2013-11-02 1 xF1yz
5 2006-07-07 5 F5spo
6 2006-07-06 5 F5spo
</code></pre>