kiasari
kiasari

Reputation: 151

Query a pandas dataframe without header

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?

Upvotes: 2

Views: 1642

Answers (1)

Laurent
Laurent

Reputation: 13498

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', ...).

Upvotes: 4

Related Questions