Home > database >  Pandas - Count repeating values by condition
Pandas - Count repeating values by condition

Time:07-20

Dataframe:

 group  val  count???
     a    2         1
     a    2         2
     b    1         1
     a    2         3
     b   -3         1
     b   -3         2
     a   -7         1
     a   -5         2

I have columns "group" and "val" and I don't know how to write pandas code to get column "count"?

The logic is like this, it should count the number of consecutive values that are on the same side (either positive or negative) grouped by column "group".

When side changes the counter should be reset to 1 and start counting again.

For example, if within one group we have numbers 1, -1, 1, 1, then the output would be 1, 1, 1, 2, since only last two values are on the same side (positive).

CodePudding user response:

You can group by group and np.sign(df['val'])

df['count'] = df.groupby(['group', np.sign(df['val'])]).cumcount().add(1)
print(df)

  group  val  count???  count
0     a    2         1      1
1     a    2         2      2
2     b    1         1      1
3     a    2         3      3
4     b   -3         1      1
5     b   -3         2      2
6     a   -7         1      1
7     a   -5         2      2
  • Related