2

I have a pandas DataFrame with no header and I have to assign string type names to the columns to be able to use the pandas query() method.

df.columns = ['A', 'B', 'C']
df.query('A > 0')

I'm wondering how can I query a DateFarame with no header?

4
  • can you give a exemple of query Commented May 7, 2021 at 13:08
  • df.query('A > 0') returns the rows in df in which the value of column A is greater than 0. Commented May 7, 2021 at 13:18
  • df [df['A'] > 0 ] Commented May 7, 2021 at 13:30
  • That's correct but doesn't use query method. Commented May 7, 2021 at 13:35

1 Answer 1

4

As per Pandas documentation, when using query: "You can refer to variables in the environment by prefixing them with an ‘@’ character"

So, just as when your dataframe has a header ('A', 'B', ...), df.query('A > 0') is equivalent to df.query("@df['A'] > 0")

When you dataframe has no header, you can query like this: df.query("@df[0] > 0") ('0' for 'A', '1' for 'B', ...).

Sign up to request clarification or add additional context in comments.

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.