0

I am trying to run a multiline query using df.query but I seem to be getting the following error even after adding backslashes:

    column = 'method'

    idx = df.query(
    f"""{column} == 'One' and \
    number.notnull() and \
    flag.isnull()""").index

My df looks like this:

df
    'method'  'number'  'flag'
23   'One'    0         None
24   'One'    1         1
25   'Two'    1         None

I get this error:

ValueError: multi-line expressions are only valid in the context of data, use DataFrame.eval

I tried to use this answer to fix but am still getting the exact same error:

pandas dataframe multiline query

Can someone help explain why this does not work?

Thanks

geds133
  • 1,503
  • 5
  • 20
  • 52

1 Answers1

1

I would suggest avoiding df.query, it is easier and more reliable to use the masking feature to filter your data. The triple quotes are also taking the indentation characters, you should avoid that.

Now, in your case,

Better syntax, with string concatenation:

column = 'method'

idx = df.query(
    f"{column} == 'One' and "
    "number_col.notnull() and "
    "flag.isnull()""").index"
)

A better solution, with filtering:

column = "method"
mask = (df[column] == "One") & df["number"].notna() & df["flag"].isna()
idx = df[mask].index