I cannot solve this simple problem, and I ask for help here ... I have a DataFrame as follows, and I want to select the first two rows in each group "a"
df = pd.DataFrame({'a':pd.Series(['NewYork','NewYork','NewYork','Washington','Washington','Texas','Texas','Texas','Texas']), 'b': np.arange(9)})
df
Out[152]:
a b
0 NewYork 0
1 NewYork 1
2 NewYork 2
3 Washington 3
4 Washington 4
5 Texas 5
6 Texas 6
7 Texas 7
8 Texas 8
those. I want to get the result as follows:
a b
0 NewYork 0
1 NewYork 1
2 Washington 3
3 Washington 4
4 Texas 5
5 Texas 6
Many thanks for the help.
source
share