擅长:python、mysql、java
<p>下面是一种使用<code>iterrows()</code>并使用<code>.at[]</code>写入数据帧的方法:</p>
<pre><code>df['start_loc'] = ''
df['end_loc'] = ''
for index, row in df.iterrows():
start = row.slotStartTime
end = row.slotEndTime
reg = row.RegistrationNo
mask = ((df2['RegistrationNo'] == reg) &
(start <= df2['GPSTime']) & (df2['GPSTime'] <= end))
df.at[index, 'start_loc'] = df2.loc[mask, 'location'].min()
df.at[index, 'end_loc'] = df2.loc[mask, 'location'].max()
print(df[['start_loc', 'end_loc']]) # other columns omitted to save space
start_loc end_loc
0 loc1 loc5
1 loc4 loc7
2 loc10 loc12
</code></pre>