How to find the change between groups in a live data, using python?


This Content is from Stack Overflow. Question asked by sam_sam

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],
        'update':[110738604,110738604,110738604,110738604, 110743097, 110743097, 110743097, 110743097, 110809881, 110509881]}
# Create DataFrame
df = pd.DataFrame(data)
# Print the output.

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

for name in df['update'].unique():
    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.


This question is not yet answered, be the first one who answer using the comment. Later the confirmed answer will be published as the solution.

This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?