Artem Reznov
Artem Reznov

Reputation: 65

Consecutive values in pandas column

I have pandas df['realize']

time                      realize
2016-01-18 08:25:00     -46.369083
2016-01-19 14:30:00     -819.010738
2016-01-20 11:10:00    -424.955847
2016-01-21 07:15:00     27.523859
2016-01-21 16:10:00     898.522762
2016-01-25 00:00:00    761.063545

Where time is:

df.index = df['time']
df.index = pd.to_datetime(df.index)

Where df['realize'] is:

In: type(df['realize'])
Out: pandas.core.series.Series

I want to count consecutive values, rule is simple (df['realize'] > 0, df['realize'] < 0)

Expected out:

time                      realize    Consecutive
2016-01-18 08:25:00     -46.369083    1
2016-01-19 14:30:00     -819.010738   2
2016-01-20 11:10:00    -424.955847    3
2016-01-21 07:15:00     27.523859     1
2016-01-21 16:10:00     898.522762    2
2016-01-25 00:00:00    761.063545     3

I read about topics about loop, but didn't find what I need. Thanks in advance for help.

Upvotes: 2

Views: 4494

Answers (2)

yatu
yatu

Reputation: 88236

You could do the following:

g = df.realize.gt(0).astype(int).diff().fillna(0).abs().cumsum()
df['Consecutive'] = df.groupby(g).realize.cumcount().add(1)

               time     realize       Consecutive
0 2016-01-18 08:25:00  -46.369083            1
1 2016-01-19 14:30:00 -819.010738            2
2 2016-01-20 11:10:00 -424.955847            3
3 2016-01-21 07:15:00   27.523859            1
4 2016-01-21 16:10:00  898.522762            2
5 2016-01-25 00:00:00  761.063545            3

Where the used grouper is obtained by taking the first differences (DataFrame.diff) of a boolean Series indicating whether or not realize is greater than 0:

diff = df.realize.gt(0).astype(int).diff().fillna(0).abs()
df.assign(diff = diff, grouper = g)

         time            realize     Consecutive diff  grouper
0 2016-01-18 08:25:00  -46.369083            1   0.0      0.0
1 2016-01-19 14:30:00 -819.010738            2   0.0      0.0
2 2016-01-20 11:10:00 -424.955847            3   0.0      0.0
3 2016-01-21 07:15:00   27.523859            1   1.0      1.0
4 2016-01-21 16:10:00  898.522762            2   0.0      1.0
5 2016-01-25 00:00:00  761.063545            3   0.0      1.0

Upvotes: 7

Bhanu Tez
Bhanu Tez

Reputation: 306

My solution.

i=0;j=0
def cons(x):
    global i;global j
    if x>0:
        i += 1;j=0
        return i
    else:
        j += 1;i=0
        return j


df['consecutive'] = df['realize'].map(lambda x: cons(x))

I hope the solution is helpful.

Upvotes: 0

Related Questions