3

How do I fix the following df.query line to stop getting the error msg: name 'z' is not defined.

I have data in 3 columns and wanna plot 3D polygons. I run a loop to pair up (X, Y), in which I try to use the loop variable, z, to screen one column:

zs = [20, 30, 40, 50, 60, 70]
for z in zs:
    ys = df.query('column1==z')['Column2']
    verts.append(list(zip(xs, ys)))

2 Answers 2

4

From your code, you may passe the value of z, not the sting "z", do thta with a formatted string for example or just concat the elements

ys = df.query(f'column1=={z}')['Column2']
ys = df.query('column1==' + str(z))['Column2']

But the slicing syntax is a bit more easy to use

ys = df[df.column1 == z]['Column2']
Sign up to request clarification or add additional context in comments.

Comments

0

Use format strings:

zs = [20, 30, 40, 50, 60, 70]
for z in zs:
    ys = df.query(f'column1=={z}')['Column2']
    verts.append(list(zip(xs, ys)))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.