krenkz
krenkz

Reputation: 470

get partial sum of values in df column once they reach a certain threshold

I need to start adding values in one of the columns in my df and return a row where the sum reaches a certain threshold. What is the easiest way to do it?

e.g.

threshold = 86

    values    ID
1   42       xxxxx
2   34       yyyyy
3   29       vvvvv
4   28       eeeee

should return line 3

Upvotes: 0

Views: 439

Answers (3)

Andy L.
Andy L.

Reputation: 25239

Another way

df['values'].cumsum().ge(threshold).idxmax()

Out[131]: 3

df.loc[df['values'].cumsum().ge(threshold).idxmax()]

Out[133]:
values       29
ID        vvvvv
Name: 3, dtype: object

Upvotes: 0

Parijat Bhatt
Parijat Bhatt

Reputation: 674

This should work

df['new_values'] = df['values'].cumsum()

rows = df[df['new_values']==threshold].index.to_list()

Upvotes: 0

Alex Hall
Alex Hall

Reputation: 36023

import pandas as pd

df = pd.DataFrame(dict(values=[42, 34, 29, 28], ID=['x', 'y', 'z', 'e']))

threshold = 86

idx = df['values'].cumsum().searchsorted(threshold)
print(df.iloc[idx])

Try it here

Output:

values    29
ID         z
Name: 2, dtype: object

Note that df.values has a special pandas meaning so df['values'] is different and necessary.

Upvotes: 1

Related Questions