0

If I use

df.loc[(origFull['abc'] == 111)]

it will work of course.

Then

test = '(origFull[\'abc\'] == 111)'

df.loc[@test]

or

df.loc['@test']

Neither worked.

3 Answers 3

1

A few things are wrong:

1: You want test to be a value(a boolean value to be exact), if you added single quotes around them, test turns into a string. Do this instead:

test = origFull['abc'] == 111

2: @ symbol isn't valid syntax for python, excepted when it's used for decorators. To use test as a variable, just write test without the @.

df.loc[test]
Sign up to request clarification or add additional context in comments.

1 Comment

actually I want test variable to represent a statement, but you are right, I do not need to add single quote. Thank you!
1

Take the quotes away when you define test.

Inside of loc you are essentially creating a boolean variable. So set test equal to that boolean and use it as a boolean index:

test = (origFull['abc'] == 111)

df.loc[test]

2 Comments

It will work after removing the back slash. Thank you very much, since you are the first one answered this question and brought me to the right direction!
Yes, sorry about that, just copied and pasted and missed the backslashes.
0

Maybe you are looking for the .query() method?

test = "abc == 111"
df.query(test)

1 Comment

in fact it's .loc, but I got the answer. thanks anyway

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.