How do I delete specific rows from each group of a dataframe?

Viewed 46

I have a csv data file that I want to process with python pandas. I want to firstly group the data based on their id. Then I want to delete the 4 smallest number in each group. Is there a way to do this?

So far I've tried:

if args.process_log:
    datafile = pd.read_csv(args.out_log) 
    grouped_data = datafile.groupby('id')
    datagroup_smallest = grouped_data.apply(lambda x: x.nsmallest(n=4, columns='width'))
    unwanted_index = datagroup_smallest.index.get_level_values(1)

Then I want to delete the unwanted rows with their index. I've tried:

data_splitted = grouped_data.filter(unwanted_index)

I got:

TypeError: 'Int64Index' object is not callable
1 Answers
Related