Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pandas:How to query multi-level column dataframe?

Tags:

python

pandas

Background:

When I do aggregate, I will sometimes end up with multi-level column dataframe, something like this:

enter image description here

so I need to query the data with multi-level columns.

Here is some dummy data

df=pd.DataFrame({'a':[1,2,3],'b':[4,5,6]})
columns=[('c','a'),('b','')]
df.columns=pd.MultiIndex.from_tuples(columns)
df

enter image description here

My question: how to query the data like b > 0, and (c,a) > 0?

I trieddf.query("b > 0 "), df.query("(c,a) > 0 "), but it returns UndefinedVariableError: name 'b' is not defined.

I also tried to search around, but didn't find things relevant, did I miss anything?

Thanks

like image 670
cqcn1991 Avatar asked Sep 01 '25 02:09

cqcn1991


1 Answers

You can use backticks for this:

df.query("`('c', 'a')` > 0 & `('b', '')` > 0")
like image 73
00schneider Avatar answered Sep 02 '25 15:09

00schneider