Here is a sample data of a larger race result dataset that I have.
import pandas as pd
# initialize data of lists.
data = {'Car': ['A', 'B', 'C', 'D','C', 'A', 'B', 'D','B','C'],
'time': [2012, 2013, 2000, 2012, 2009, 2012, 2013,2000, 2002, 1999],
'pos':[2,1,1,1,3,2,2,2,3,1],
'update':[110738604,110738604,110738604,110738604, 110743097, 110743097, 110743097, 110743097, 110809881, 110509881]}
# Create DataFrame
df = pd.DataFrame(data)
# Print the output.
df
This dataset gets updated every few seconds(not in a fix interval I guess), I want to find the Top car for every update and return a message that which car is leading, also in the next update if the top car is faster than previous top one then change the top car announcement.
I did
df_sliced={}
for name in df['update'].unique():
df_sliced[name]=df[df['update']==name]
df_sliced[name] = df_sliced[name].sort_values('time')
top = df_sliced[name]['time'].idxmin()
print('the top car is {} for update {}.'.format(df_sliced[name]['Car'].loc[top], df_sliced[name]['update'].loc[top]))
I tried to first add a new column to display word ‘top’ if the second group top be faster than first group,
df_sliced[name]['top'] = df_sliced[name]['time'].loc[[top]].values > df_sliced[name]['time'].min()
yet stoked and don’t know how to do this.Please help.