Trim dataframe by total of column

Question:

Let’s say that I have a data frame with a column named subtotal, and I want to extract a sub-data frame with a maximum sum of all subtotal

l = [100, 200, 300, 400, 500]

df = pd.DataFrame(l)

Here, I want to get a df with maximum value of 1000, in this case, should return [100, 200, 300, 400]

How can I do this using panda?

Asked By: Rodrigo

||

Answers:

here is one way to do it

#take cumsum and choose only rows that are at or below the threshold

df.loc[df[0].cumsum()<=1000]
0
0   100
1   200
2   300
3   400
Answered By: Naveed
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.