0

I need some help with the following problem:

the data looks like

dt  value
15  0
15  2   
15  8   
15  8   
15  10  
16  12
15  19  
15  35  
15  45
16  45  
16  45  
15  50
15  0
16  26  
15  43
15  50  
15  0
.
.
.

now I have to sum up dt until value reaches 50, always beginning from 0.

I have tried the following, but I am not sure if it is right,

df['value'].values[(df['value'].values > 0) & (df['value'].values < 50)] = 1    
df =  df.assign(counter_col_x = df.loc[df['value'].eq(1)].groupby(df['value'].ne(df['value'].shift()).cumsum()).ngroup())

Thanks for any hints!

5
  • 1
    Does the series always end at 50? Or there might be data after? Commented Feb 20, 2020 at 18:54
  • 1
    could you show expected output? Commented Feb 20, 2020 at 18:55
  • 1
    df.loc[df['value'].between(0,50, inclusive = False),'dt'].groupby(df['value'].ge(50).cumsum()).sum()? or size instead sum() Commented Feb 20, 2020 at 19:00
  • @Quang Hoang no it could also be any other value. Expected output is the sum from 0 to 50 and then start again from zero to 50. This should be a new column Commented Feb 20, 2020 at 19:10
  • 1
    df['value'].values[(df['value'].values > 0) & (df['value'].values < 50)] = 1 Won't that work without the .values ? Commented Feb 20, 2020 at 19:28

1 Answer 1

1

A simple way if you didn't have to restart several time within a column would have been the cumulating sum of a series. https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.cumsum.html

I hope you meant a cumulating sum of values until it reaches 50. I could only come up with a way of assigning the value of the Series in a loop. Hope that helps.

new_cum_sum = 0
list_of_values = []
for x, y in zip(test['dt'], test['value']):
    if y== 0:
        new_cum_sum = x
    elif y <= 50:
        new_cum_sum+=x
    list_of_values.append(new_cum_sum)

test['test_cum'] = list_of_values
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.