Asking help of pandas as groupby function?

Viewed 44

enter image description here

As my new comer for python using, could help me how to create the "Number column" by number sequence under different name of "test1" column, thanks. (for example: pandas groupby function??)

1 Answers

Try:

df["Number"] = (df.test1 != df.test1.shift()).cumsum()
print(df)

Prints:

   test1  Number
0    AAA       1
1    AAA       1
2    AAA       1
3    AAA       1
4    BBB       2
5    BBB       2
6    BBB       2
7    AAA       3
8    AAA       3
9    AAA       3
10   CCC       4
11   CCC       4
Related