1

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  
cs95
  • 379,657
  • 97
  • 704
  • 746
Krishna
  • 13
  • 4
  • Please explain your output. It does not make much sense. – cs95 Mar 14 '18 at 18:42
  • Hi, I am trying to count the number of occurances of negative values and then the positive values that are immediately following negative values. So, if you take my y array,( y = [1,-1,-1,1,1,1,1,-1,-1]), the first value is positive, so the count is 1 and then followed by 2 negative values, so the count_neg = 2 and then follwed by 4 positive values. – Krishna Mar 14 '18 at 18:49
  • I got the answer. I am able to append using the following code df_count.loc[i,'count_pos'] = count_pos – Krishna Mar 14 '18 at 18:50

2 Answers2

3

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
cs95
  • 379,657
  • 97
  • 704
  • 746
  • This code tripped when there was an odd no. of consecutive counts. Ex: `a = pd.Series([1, -2, -15, 3, 45, -5, -23, 3, 6, -4, 8, 5, 3, -9, -7, -36, -71, -2, 25, 47])` – rahul-ahuja Jul 16 '21 at 05:43
0

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.

rahul-ahuja
  • 1,166
  • 1
  • 12
  • 24