.head () and .tail () with negative indices on the pandas GroupBy object

I am having problems filtering everything except the last 1 element in each groupby groupby object from pandas.DataFrame:

x = pd.DataFrame([['a', 1], ['b', 1], ['a', 2], ['b', 2], ['a', 3], ['b', 3]], 
                 columns=['A', 'B'])
g = x.groupby('A')

As expected (according to the documentation ) g.head(1)returns

   A  B
0  a  1
1  b  1

whereas it g.head(-1)returns an empty DataFrame

From behavior x.head(-1)I expect him to return

   A  B
0  a  1
1  b  1
2  a  2
3  b  2

i.e. discarding the last element of each group and then combining it back into a dataframe. If this were a mistake in pandas, I would be grateful to everyone who offers an alternative approach.

+4
source share
2 answers

, pandas. cumcount :

def negative_head(g, n):
    return g._selected_obj[g.cumcount(ascending=False) >= n]

def negative_tail(g, n):
    return g._selected_obj[g.cumcount() >= n]

In [11]: negative_head(g, 1)  # instead of g.head(-1)
Out[11]:
   B
0  1
1  1
2  2
3  2
+2

@TomAugspurger, pandas, g.apply(lambda x: x.head(-1)) , :

    A  B
A        
a 0  a  1
  2  a  2
b 1  b  1
  3  b  2

.reset_index(drop=True) (mind drop = True )

   A  B
0  a  1
1  a  2
2  b  1
3  b  2

, g.apply(lambda x: x.head(-1)).reset_index(drop=True)

+1

Source: https://habr.com/ru/post/1616390/


All Articles