A = pd.DataFrame({"type":['a','b','c', 'd','e'], "cost basis":[50, 40, 30, 20, 10], "value":[5, 25, 40, 10, 20]})
I am looking to iterate over the "value" column up to a certain value or sum in descending order. Let's say 50, whereas if the next number exceeds that value then the iteration would stop there.
Not sure that what you want but If I Understand correctly:
Try via cumsum()
:
out=A.loc[A['value'].cumsum().le(50)]
OR
If want in descending order then use sort_values()
+cumsum()
:
out=A.loc[A.sort_values('value',ascending=False,ignore_index=True)['value'].cumsum().le(50)]