Mickel
Mickel

Reputation: 6696

How to use Pandas query() method on a MultiIndex DataFrame?

I have a pandas DataFrame that has the following MultiIndex on list, conId:

                 secType  symbol lastTradeDateOrContractMonth  strike right multiplier  ... includeExpired secIdType secId comboLegsDescrip comboLegs  deltaNeutralContract
list   conId                                                                            ...
a-list 917920        STK  VOLV.B                                  0.0                   ...          False                                         []                  None
       263751353     STK   SBB.B                                  0.0                   ...          False                                         []                  None

According to the documentation on MultiIndex query() syntax, I should be able to query on the index as well, like so...

securities = [917920]
df.query('list == "a-list" and conId == @securities')

But this results in KeyError: False.

Upvotes: 1

Views: 39

Answers (1)

BENY
BENY

Reputation: 323226

When you create the index name try not use the python or pandas exist function.(list is build-in function)

df=df.rename_axis(['l','conId']) # fix it with rename_axis
securities = [917920]
df.query('list == "a-list" and conId == @securities')

Upvotes: 2

Related Questions