Closed
Description
I'm not sure what a good word for this is (count is taken, order means sort)!
But it's quite an often used thing to create a column which enumerates the items in each group / counts their occurrences.
You can hack it:
In [1]: df = pd.DataFrame([[1, 2], [2, 3], [1, 4], [1, 5], [2, 6]])
In [2]: g = df.groupby(0)
In [3]: g.apply(lambda x: pd.Series(np.arange(len(x)), x.index))
Out[3]:
0 0
1 0
2 1
3 2
4 1
dtype: int64
In [5]: df['order'] = _
In [6]: df
Out[6]:
0 1 order
0 1 2 0
1 2 3 0
2 1 4 1
3 1 5 2
4 2 6 1
I've seen this in a few SO questions, here's just one.
cc @cpcloud (and I've seen @jreback answer a question with this)