Q:

The order of duplicate values in the pandas DataFrame column

How I can add new column in pandas DataFrame which will numerate all repeated values.  In example below I have 3 values from 865432 in column be, so as output I add column and fill values 0, 1 and 2 for this rows. 
Input table
A       B
0  865432
1  865432
2  134567
3  865432
4  134567
5  222222

Output table
A       B  C
0  865432  0
1  865432  1
3  865432  2
2  134567  0
4  134567  1
5  222222  0

Note: Order and new column name doesn't metter. 
I know how to do this task in ugly loops and row manipulation, but hope there is some another more beautiful solution in pandas. 

A:

You can groupby on 'B' column and then call cumcount:
In [345]:

df['C'] = df.groupby('B', as_index=False)['B'].cumcount()
df
Out[345]:
   A       B  C
0  0  865432  0
1  1  865432  1
2  2  134567  0
3  3  865432  2
4  4  134567  1
5  5  222222  0

