I would like to select rows in a dataframe based on a sum crieteria of one of the columns. For example I want the indexes of the the first rows of the dataframe where the sum of column B is less than 3:
df = pd.DataFrame({'A':[z, y, x, w], 'B':[1, 1, 1, 1]})
The only solution I have is a seperate dataframe and a while loop:
df2 = pd.DataFrame({'A':[], 'B':[]})
index = 0
while df2['B'].sum() < 3:
df2 = df2.append(df1.loc[index])
index += 1
The logic gets me where I need but seems unnecessarily inefficient. Does anyone have a creative way of using pandas to filter the dataframe based on sum conditional of a column?