擅长:python、mysql、java
<p>您可以在<code>groupby()</code>上进行筛选和排序,然后重新分配:</p>
<pre><code>df['if_top_ten_height_rank'] = (df.loc[df['top_ten_finish']=='yes','height_in_inches']
.groupby(df['race_date']).rank(ascending=False)
.reindex(df.index, fill_value=0)
.astype(int)
)
</code></pre>
<p>输出:</p>
<pre><code> runner race_date height_in_inches top_ten_finish if_top_ten_height_rank
-
0 mike 2019-02-02 72 yes 1
1 paul 2019-02-02 68 yes 2
2 jim 2020-02-02 70 no 0
3 dave 2020-02-01 74 yes 1
4 douglas 2020-02-01 73 no 0
</code></pre>