0

Having Pandas dataframe with one column like that:

enter image description here

I would like to create ID column based on groupby() logics of time column like this:

enter image description here

Know how to do that incrementally: using index as a column or using groupby() with rank function:

df['ID'] = df.groupby('time')['time'].rank(method='first')

But how can it be done using the requested way?

Thanks!

Keithx
  • 2,994
  • 15
  • 42
  • 71

1 Answers1

2

Based on the input you could make use of pd.factorize():

df['ID']=pd.factorize(df.time)[0]
print(df)

       time  ID
0  01.09.16   0
1  01.09.16   0
2  01.09.16   0
3  02.09.16   1
4  02.09.16   1
5  02.09.16   1
6  03.09.16   2
7  03.09.16   2
8  03.09.16   2
anky
  • 74,114
  • 11
  • 41
  • 70