zathura
zathura

Reputation: 71

Count rows based on a condition

Here is a very small subset of my dataframe. Original dataframe is very large

    df = pd.DataFrame({
               'XX_111':[-14,-90,-90,-96,-91,-5,-98,-74,-96,-45,-11,-54,-45],
               'YY_222':[-103,0,-110,-114,-114,-113,-114,-115,-113,-111,-112,-122,-113],
               'ZZ_111':[1,2,3,5,6,6,7,7,4,8,9,2,6],
               'value':[1,1,2,3,3,1,2,2,2,3,3,1,1]
    })

You see that the values under column 'value' are always in the order 1,2,3. What I want is to create a new column 'id' and fill it like this;

    value      id
     1         1
     1         1
     2         1
     3         1        
     3         1
     1         2
     2         2
     2         2
     2         2
     3         2
     3         2
     1         3
     1         3

So everytime the value changed from 3 to 1, I want it to increment the id by 1. Is there a way to accomplish this and also efficiently?

Upvotes: 0

Views: 95

Answers (2)

Zero
Zero

Reputation: 76917

I'd use

In [166]: df['id'] = (df.value.shift().eq(3) & df.value.eq(1)).cumsum() + 1

In [167]: df
Out[167]:
    XX_111  YY_222  ZZ_111  value  id
0      -14    -103       1      1   1
1      -90       0       2      1   1
2      -90    -110       3      2   1
3      -96    -114       5      3   1
4      -91    -114       6      3   1
5       -5    -113       6      1   2
6      -98    -114       7      2   2
7      -74    -115       7      2   2
8      -96    -113       4      2   2
9      -45    -111       8      3   2
10     -11    -112       9      3   2
11     -54    -122       2      1   3
12     -45    -113       6      1   3

Note: Don't use diff if you have any number pairs with difference 2. For example 5, 3, x and so on.

Upvotes: 2

Scott Boston
Scott Boston

Reputation: 153460

Use:

df['id'] = df['value'].diff().eq(-2).cumsum() + 1

Output:

    XX_111  YY_222  ZZ_111  value  id
0      -14    -103       1      1   1
1      -90       0       2      1   1
2      -90    -110       3      2   1
3      -96    -114       5      3   1
4      -91    -114       6      3   1
5       -5    -113       6      1   2
6      -98    -114       7      2   2
7      -74    -115       7      2   2
8      -96    -113       4      2   2
9      -45    -111       8      3   2
10     -11    -112       9      3   2
11     -54    -122       2      1   3
12     -45    -113       6      1   3

Upvotes: 3

Related Questions