st19297
st19297

Reputation: 629

Query a data frame by multiple columns?

I can't figure this error out.

df.info()
<class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 1048575 entries, 1966-03-31 to 1994-03-31
Data columns (total 24 columns):
gvkey         1048575 non-null int64
tic           1048575 non-null object

df.query('(gvkey==1690) & (mkt_val> 400)')['2015-03-31':]

              gvkey   tic       conm   mkt_val>
datadate                                                                       
2015-03-31    1690  AAPL  APPLE INC     600
.
.
.

As you can see, there is a column 'tic', and a value 'AAPL' for it. So, why does the below query return as error? Isn't it almost the same as the above query?

df.query('(tic=='AAPL') & (mkt_val> 400)')['2015-03-31':]

  File "<ipython-input-386-34ae806044b9>", line 1
    df.query('(tic=='AAPL') & (mkt_val> 400)')['2015-03-31':]
                        ^
SyntaxError: invalid syntax

In general, I have this large data set, with the dates column as the index. I always need to query different companies (tic) by different criteria (mkt_val> 400 in this case). I always get confused when indexing based on multiple criteria. Shall I make the dataset multi-index (by date and tic). Will that make my job easier?

Upvotes: 2

Views: 2625

Answers (1)

ℕʘʘḆḽḘ
ℕʘʘḆḽḘ

Reputation: 19395

try this brother

'(tic=="AAPL")

note how I use " not to confuse Python with '

Upvotes: 3

Related Questions