擅长:python、mysql、java
<p>我们可以用<code>cumsum</code>和<code>groupby</code></p>
<pre><code>df['recent']=df.finishing_position.astype(str)+'/'
df['recent']=df.groupby('horse_id').recent.apply(lambda x : x.cumsum().shift().str[:-1].fillna(''))
df
Out[140]:
finishing_position horse_id race_id recent
0 1 K01 2014011
1 2 K02 2014011
2 3 M01 2014011
3 4 K01 2014012 1
4 2 K01 2014021 1/4
5 3 K01 2014031 1/4/2
6 1 M01 2015011 3
7 2 K01 2016012 1/4/2/3
8 1 K02 2016012 2
9 3 M01 2016012 3/1
10 4 J01 2016012
</code></pre>