Krishna
Krishna

Reputation: 13

Count consecutive positive and negative values in a list

I created a Dataframe with two columns and would like to append them based on the counting of values from other array.

cols = ['count_pos','count_neg']
df_count = pd.DataFrame(columns=cols)

I have the array y with values like y = [1,-1,-1,1,1,1,1,-1,-1]

Now i want to update for every change in value in y, count those occurrences and append to respective columns.

for i in range(1,10):

    if y[i] == -1:
        print(y[i])

        if count_pos > 0:
            df_count.loc['count_pos'].append = count_pos

        count_pos = 0
        count_neg = count_neg - 1

    else:    
        if count_neg< 0:
            print(count_neg)
        df_count.loc['count_neg'].append = count_neg
        count_neg = 0
        count_pos = count_pos + 1

But I am not getting the result.Please let me know how can I append values to dataframe column.

My desired output is df_count

count_pos  count_neg 
1             -2
4             -2  

Upvotes: 1

Views: 1220

Answers (2)

rahul-ahuja
rahul-ahuja

Reputation: 1424

Adapted from @cs95's answer:

a = pd.Series([-1, 2, 15, 3, 45, 5, 23, 0, 6, -4, -8, -5, 3, 
-9, -7, -36, -71, -2, 25, 47, -8])

def pos_neg_count(a):
    v = a.ge(0).ne(a.ge(0).shift()).cumsum()
    vals = v.groupby(v).count().values
    cols = ['pos', 'neg'] if a[0] >= 0 else ['neg', 'pos']
    try:
        result = pd.DataFrame(vals.reshape(-1, 2), columns=cols)
    except ValueError:
        vals = np.insert(vals, len(vals), 0)
        result = pd.DataFrame(vals.reshape(-1, 2), columns=cols)
    return result

pos_neg_count(a)
#       neg pos
#   0     1   8
#   1     3   1
#   2     5   2
#   3     1   0

I think, this would take care of cases where the array being reshaped has odd no. of elements.

Upvotes: 0

cs95
cs95

Reputation: 402483

Count consecutive groups of positive/negative values using groupby:

s = pd.Series(y)
v = s.gt(0).ne(s.gt(0).shift()).cumsum()

pd.DataFrame(
    v.groupby(v).count().values.reshape(-1, 2), columns=['pos', 'neg']
)

   pos  neg
0    1    2
1    4    2

Upvotes: 3

Related Questions