<p>这是一个相当复杂的问题,我的观点是:</p>
<pre><code># Add the Driver columns by shifting grouped by the Event
df['new'] = df.groupby('Event').apply(lambda x : x['Driver'].shift(1) +'to'+ x['Driver']).values
# Split them by 'to'
df['new'] =df['new'].str.split('to').bfill()
# Check if both of them are equal
m = df['new'].str[0] != df['new'].str[1]
# Based on the condition create a new dataframe
new_df = df.loc[m].copy().iloc[:-1]
# Convert the list to the format you desired
new_df['new'] = new_df['new'].str[0] + ' to ' + new_df['new'].str[1]
# Concat new dataframe and old dataframe
mdf = pd.concat([df.drop('new',1) , new_df.drop(['Driver','Event'],1) \
.rename(columns = {'new':'Event'})])
Car_ID Date Driver Event
0 B332 2018-03-12 Alex Rent_Car_B
1 B332 2018-03-14 Alex Rent_Car_B
2 B332 2018-03-15 Mick Rent_Car_B
3 C315 2018-03-17 Sara Rent_Car_C
4 C315 2018-03-13 Sara Rent_Car_C
5 C315 2018-03-15 Jean Rent_Car_C
6 C315 2018-03-18 Sara Rent_Car_C
7 C315 2018-03-21 Sara Rent_Car_C
8 F310 2018-03-10 Franck Rent_Car_F
9 F310 2018-03-13 Michel Rent_Car_F
2 B332 2018-03-15 NaN Alex to Mick
5 C315 2018-03-15 NaN Sara to Jean
6 C315 2018-03-18 NaN Jean to Sara
8 F310 2018-03-10 NaN Franck to Michel
</code></pre>
<p>如果你想要顺序,那么对索引进行排序</p>
<pre><code>mdf = mdf.sort_index()
</code></pre>