user308827
user308827

Reputation: 22041

Keep running count of number of values greater than threshold in pandas dataframe

In the dataframe below:

    va
0   35
1   12
2   24
3   25
4   26
5   19
6   14
7   22
8   35
9   35
10  29
11  13
12  20
13  10
14  10
15  23
16  11
17  30
18  26
19  32
20  11

I would like to keep a running count of number of rows where the va column value exceeds 30. I was thinking of using value_counts but that does not seem to be right

Upvotes: 2

Views: 4977

Answers (2)

jezrael
jezrael

Reputation: 863741

There are 2 solutions - with count reset to column new and another solution without reset to new1:

a = df['va'].gt(30)
b = a.cumsum()
df['new'] = b-b.mask(a).ffill().fillna(0).astype(int)
df['new1'] = b.where(a, 0)
print (df)
    va  new  new1
0   35    1     1
1   12    0     0
2   24    0     0
3   25    0     0
4   26    0     0
5   19    0     0
6   14    0     0
7   22    0     0
8   35    1     2
9   35    2     3
10  29    0     0
11  13    0     0
12  20    0     0
13  10    0     0
14  10    0     0
15  23    0     0
16  11    0     0
17  30    0     0
18  26    0     0
19  32    1     4
20  11    0     0

Upvotes: 4

PL200
PL200

Reputation: 741

To get number of rows, you can do the following:

your_counter = len(your_df[your_df['va'] > 30])

('your_df' obviously should be replaced by the name of your dataframe)

What the code is doing is creating a new dataframe, only containing rows where the value of 'va' is over 30. Then the 'len' function is counting the number of rows.

Upvotes: 1

Related Questions