Patricia,
For boolean indexing in pandas, or querying, I've learned a lot here in last 24, (but not this. This was done in 1 hour since coming home from an important day out).
Truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()
dataquery= df[(df['X_3'] == 'Yes') | (df['X_3'] == 'Maybe')]
use & (for "and") and | (for "or") and make sure the "(" & ")" are well arranged for the boolean logic to work, which im sure youve worked out by now without me, but just posting in for completenes.
so full "fun - test code" for nb's like you and I is:
import pandas as pd
data = {'X_1': [1, 10, 2, 5, 0], 'X_2': [11, 30, 16, 31, 31], 'X_3': ['Yes', 'Maybe', 'No', 'Yes', 'No']}
df = pd.DataFrame(data)
print("Hello start")
print(df)
def mysplit4 (dataframe):
print("hello 1")
#def mysplit (a):
df1 = df[df['X_3'] == 'Yes']
df2 = df[df['X_3'] == 'No']
dataquery= df[(df['X_3'] == 'Yes') | (df['X_3'] == 'Maybe')]
print(df1)
print("hello 2")
print(df2)
print("hello 3")
print(dataquery)
print("hello 4")
return
mysplit4(df)
ref0
ref1 <--- all good, but this was the Tick/coin drop for me :)
ref2 (this even looks even better explination, which i never refered to until finishing the code)
Last word of warning: No one on so is supposed answer or try to answer questions like I do, or done so, (i think) - too personable/sociable/'wafly' with irrelent information they say (and some may well be irrelevent, indeed, or it might be helpful, idk) so I may get mark down because of that if someone else sees my answer (idk).
But I wanted or let you know about and how to employ and use booleans the way you wanted to (and asked) , and not need groupby which I too personally do not favour. So the above code imo is better . (but you can use groupby & booleans combined, up to you)
last thing, as @edchum in ref0 (see below), and others say, it is inefficient to try iterating over a dframe, because that's already done for you and inherent in pandas.
(I'm not the first to say and repeating what I've learned) but intuitively I already knew that. See this interesting redit post for eg.