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.
source
share