Search code examples
pythonpandasdataframegroup-by

Pandas group by row based values based on condition


I need to find event names, date and value (of the column) which meet a condition from the below data using Pandas. I want to find the if any events processed value is less than it's own average value for that particular event.

Original Data

The condition is for any event_name,

number_of_items_processed < avg(number_of_items_processed)

The output I am looking for is,

Output for each event


Solution

  • Use from this code:

    df['m']=df.groupby(by=['event'])['number_of_processed'].transform('mean')
    
    df=df[df.number_of_processed<df.m]