Home > Net >  How can I remove the lines that after the 'events' first equals 1 for each id?
How can I remove the lines that after the 'events' first equals 1 for each id?

Time:01-07

This is my data: enter image description here

Now here is what you need:

def remove_rows(df):
    df = df.reset_index(drop=True)
    if df[df['events'] == 1].index.empty:
        return df
    return df.loc[:df[df['events'] == 1].index[0]]

result = df.groupby('id').apply(lambda x: remove_rows(x)).reset_index(drop=True)
print(result)

Which will give you:

enter image description here

  • Related