I have a sorted pandas data frame like the following:
ID Values
'AAAA' 1
'AAAA' 2
'AAAA' 3
'BBBB' 4
'CCCC' 5
'CCCC' 6
I have to create a new column "ID_Index"
which will be like this
ID Values ID_Index
'AAAA' 1 1
'AAAA' 2 1
'AAAA' 3 1
'BBBB' 4 2
'CCCC' 5 3
'CCCC' 6 3
That means the code will check ID of row 1, it will start counting as 1. Then whenever there is a new id the counter will be +1.
In SAS we used to do it using first.id
and/or last.id
. Here I found that using shift() we can create that. I tried the following code in python, but it is not working.
c=1
for index, row in df_pandas.iterrows():
if (df_pandas['ID'] == df_pandas['ID'].shift()):
df_pandas['ID_Index']=c
else:
df_pandas['ID_Index'] = c+1
print df_pandas