Home > Software engineering >  Trim dataframe by total of column
Trim dataframe by total of column

Time:10-21

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?

CodePudding user response:

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
  • Related