Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to drop row at certain index in every group in GroupBy object?

I'm trying to drop a row at certain index in every group inside a GroupBy object.

The best I have been able to manage is:

import pandas as pd
    
x_train = x_train.groupby('ID')
x_train.apply(lambda x: x.drop([0], axis=0))

However, this doesn't work. I have spent a whole day on this to no solution, so have turned to stack.

Edit: A solution for any index value is needed as well

like image 846
Aayush Panda Avatar asked Sep 17 '20 01:09

Aayush Panda


2 Answers

You can do it with cumcount

idx= x_train.groupby('ID').cumcount()
x_train = x_train[idx!=0]
like image 70
BENY Avatar answered Oct 12 '22 02:10

BENY


The problem with using drop inside the groupby is the index numbers are still the same as before the groupby. So when using drop([0]), only the row that originally had 0 as index will be dropped. In the other groups, there will not be any row with index 0 as long as the index is unique.

If you want to use drop then what you can do is to first use reset_index inside the grouped data:

x_train.groupby('ID').apply(lambda x: x.reset_index().drop([0]))
like image 1
Shaido Avatar answered Oct 12 '22 02:10

Shaido