Let's say I create a simple dataframe and I add on some columns
import pandas as pd
data = {
'price': [6, 5.5, 5, 4.8],
'amount': [10, 12, 8, 6]
}
df = pd.DataFrame(data=data)
df['total'] = df.price * df.amount
df['running_total'] = df.total.cumsum().round(2)
my dataframe now looks like this:
amount price total running_total
0 10 6.0 60.0 60.0
1 12 5.5 66.0 126.0
2 8 5.0 40.0 166.0
3 6 4.8 28.8 194.8
I want the row from the dataframe with running total just under 160:
row_under_160 = df[df['running_total'] < 160].max()
This row (series) looks like this:
amount 12.0
price 6.0
total 66.0
running_total 126.0
dtype: float64
All the values look good EXCEPT PRICE. For some reason it's returning the top price (6.0) when it should return 5.5 which is the corresponding value.
Am I missing something/doing this entirely wrong?