I have a below Dataframe
Col1 Col2 Col3
A ABC 100
B BCD 200
C CDA 300
D CDA 400
E CDA 500
F EFG 600
G XYZ 700
H XYZ 800
I PQR 900
We can see that in Col2 CDA is repeated 3 times and XYZ is repeated 2 times.
Now I need create the new column NewCol4 and check for the consecutive values, if there are consecutive values just add underscore at the last and add the number after underscore, if it is not consecutive value leave as it is.
I need the output in the below format.
Col1 Col2 Col3 NewCol4
A ABC 100 ABC
B BCD 200 BCD
C CDA 300 CDA_1
D CDA 400 CDA_2
E CDA 500 CDA_3
F EFG 600 EFG
G XYZ 700 XYZ_1
H XYZ 800 XYZ_2
I PQR 900 PQR
I am very new to Pandas, is there any way to achieve the above output that it would be really helpful.