Reputation: 1698
I have a dataframe like this:
Col1 | Col2 | Col3
a | 8 | 9
a | 3 | 7
a | 1 | 3
a | 0 | 8
b | 6 | 18
b | 2 | 6
I would like to drop everything but the 2nd and 3rd top value for Col2, by grouping Col1, assuming it's possible
Output desired:
Col1 | Col2 | Col3
a | 3 | 7
a | 1 | 3
b | 2 | 6
Upvotes: 0
Views: 97
Reputation: 323306
It is possible by using cumcount
df[df.groupby('Col1').cumcount().isin([1,2])]
Out[423]:
Col1 Col2 Col3
1 a 2 7
2 a 1 3
5 b 2 6
More information :
df.groupby('Col1').cumcount()
Out[435]:
0 0
1 1
2 2
3 3
4 0
5 1
dtype: int64
Upvotes: 1