I have a data frame like this
df
col1 col2
A 1
B 1
C 1
D 4
E 4
F 3
G 6
H 4
I 4
Now I want to create another data frame from above with a new column col3 where previous not same value of col2 will be present, so final data frame will look like,
col1 col2 col3
A 1 1
B 1 1
C 1 1
D 4 1
E 4 1
F 3 4
G 6 3
H 4 6
I 4 6
I could do this using a for loop and checking with the next, but the execution time will be more, looking for some pandas shortcuts/ pythonic way to do this most efficiently.