agriphar
agriphar

Reputation: 31

Pandas: How to add a new column of Group ID to a dataframe based on the loop of a column of index sequence

I have a dataframe like this :

Index Column1 Column2
0 xxxxxxx yyyy
1 xxxxxx yyy
2 xxxxx yy
0 xxx y
1 xx yyyyy

What I want is to add a new column to represent Group ID based on the loop of column Index. A new Group ID is required when Index resets to 0.

like this:

Index Column1 Column2 Group ID
0 xxxxxxx yyyy 1
1 xxxxxx yyy 1
2 xxxxx yy 1
0 xxx y 2
1 xx yyyyy 2

Please give me instructions, thanks

Upvotes: 0

Views: 378

Answers (1)

SeaBean
SeaBean

Reputation: 23217

As you mentioned:

represent Group ID based on the loop of Index

If you mean that a new group is formed whenever Index reset to 0, you can try:

df['Group ID'] = df['Index'].eq(0).cumsum()

Or, if you mean a new group is formed whenever Index stop from an increasing sequence and starts from an index value smaller than the previous index, you can use:

df['Group ID'] = df['Index'].diff().lt(0).cumsum() + 1

Result:

print(df)

   Index  Column1 Column2  Group ID
0      0  xxxxxxx    yyyy         1
1      1   xxxxxx     yyy         1
2      2    xxxxx      yy         1
3      0      xxx       y         2
4      1       xx   yyyyy         2

Upvotes: 1

Related Questions