all
I have a data frame as below
df = pd.DataFrame.from_dict({'A':[1,1,2], 'B':[10,20,14], "C":[30,40,20]})
How to create a new column for example newCol
which depends on column A
. if value at A
remains same, then newCol
will fill with a increasing integer. if there is a diff value in A
, here 2
, then the value of newCol
will reset and start increasing again.
Thank you.
Code
grp = df['A'].ne(df['A'].shift()).cumsum()
df['newCol'] = df['A'].groupby(grp).cumcount().add(1)
df:
A B C newCol
0 1 10 30 1
1 1 20 40 2
2 2 14 20 1