简体   繁体   中英

Selecting Rows in pandas multiindex using query

I have the following dataframe:

 Attributes Adj Close   
Symbols ADANIPORTS.NS   ASIANPAINT.NS   AXISBANK.NS BAJAJ-AUTO.NS   BAJFINANCE.NS   BAJAJFINSV.NS   BHARTIARTL.NS   INFRATEL.NS BPCL.NS BRITANNIA.NS    ... TCS.NS  TATAMOTORS.NS   TATASTEEL.NS    TECHM.NS    TITAN.NS    ULTRACEMCO.NS   UPL.NS  VEDL.NS WIPRO.NS    ZEEL.NS
month   day                                                                                 
1   1   279.239893  676.232860  290.424052  2324.556588 974.134152  3710.866499 290.157978  243.696764  146.170036  950.108271  ... NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
2   240.371331  507.737111  236.844831  2340.821987 718.111446  3042.034076 277.125503  236.177303  122.136606  733.759396  ... -2.714824   2.830603    109.334502  -17.856865  13.293902   18.980020   0.689529    -0.006994   -3.862265   -10.423989
3   241.700116  498.997079  213.632179  2368.956136 746.050460  3292.162304 279.075750  231.213816  114.698633  686.986466  ... 0.075497    -0.629591   -0.241416   -0.260787   1.392858    -1.196444   -0.660421   -0.161608   -0.243293   -1.687734
4   223.532480  439.849441  201.245454  2391.910913 499.554044  2313.025635 287.582485  276.568762  104.650728  603.446742  ... -1.270405   0.178012    0.109399    -0.224380   -0.415277   -5.050810   -0.084462   -0.075032   3.924894    0.959136
5   213.588413  359.632790  187.594303  2442.596619 309.180993  1587.324934 260.401816  305.384079  95.571235   475.708696  ... -0.995601   -1.093621   0.214684    -1.189623   -2.503186   -0.511994   -0.512211   0.693024    -1.025715   -1.516946
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
12  27  238.901700  500.376711  227.057510  2413.230611 748.599821  3299.320564 276.806537  242.597250  124.235449  727.263012  ... 2.770155    -4.410527   -0.031403   -5.315438   -1.792164   1.038870    -0.860125   -1.258880   -0.933370   -1.487581
28  236.105050  461.535601  218.893424  2375.671582 542.521903  2613.480190 284.374906  264.309625  117.807956  681.625725  ... -0.614677   -1.045941   0.688749    -0.375988   1.848569    -1.362454   37.301528   4.794349    -21.079648  -2.224608
29  215.606034  372.030459  203.876520  2450.112244 324.772498  1765.010912 257.278008  300.096024  108.679112  543.112336  ... 3.220893    -28.873421  0.197491    0.649738    0.737047    -6.121189   -1.165286   0.197648    0.250269    -0.064486
30  205.715512  432.342895  235.872734  2279.715479 515.535031  2164.257183 237.584375  253.401642  116.322402  634.503822  ... -1.190093   0.111826    -1.100066   -0.274475   -1.107278   -0.638013   -7.148901   -0.594369   -0.622608   0.368726
31  222.971462  490.784491  246.348255  2211.909688 670.891505  2671.694809 260.623987  230.032092  108.617400  719.389436  ... -1.950700   0.994181    -11.328524  -1.575859   -8.297147   1.151578    -0.059656   -0.650074   -0.648105   -0.749307
366 rows × 601 columns

To select the row which is month 1 and day 1 i have used the following code:

df.query('month ==1' and 'day ==1')

But this produced the following dataframe:

    Attributes  Adj Close    
Symbols ADANIPORTS.NS   ASIANPAINT.NS   AXISBANK.NS BAJAJ-AUTO.NS   BAJFINANCE.NS   BAJAJFINSV.NS   BHARTIARTL.NS   INFRATEL.NS BPCL.NS BRITANNIA.NS    ... TCS.NS  TATAMOTORS.NS   TATASTEEL.NS    TECHM.NS    TITAN.NS    ULTRACEMCO.NS   UPL.NS  VEDL.NS WIPRO.NS    ZEEL.NS
month   day                                                                                 
1   1   279.239893  676.232860  290.424052  2324.556588 974.134152  3710.866499 290.157978  243.696764  146.170036  950.108271  ... NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
2   1   215.752040  453.336287  213.741552  2373.224390 517.295897  2289.618629 280.212598  253.640594  104.505893  620.435294  ... -2.526060   -1.059128   -2.052233   3.941005    25.233763   -41.377432  1.032536    7.398859    -4.622867   -1.506376
3   1   233.534958  472.889636  204.900776  2318.030298 561.193189  2697.357413 254.006857  250.426263  106.528327  649.475321  ... -2.269081   -1.375370   -1.734496   27.675276   -1.944131   0.401074    -0.852499   -0.119033   -1.723600   -1.930760
4   1   192.280787  467.604906  227.369618  1982.318034 506.188324  1931.920305 252.626459  226.062386  98.663596   637.086713  ... -0.044923   -0.111909   -0.181328   -1.943672   1.983368    -1.677000   -0.531217   0.032385    -0.956535   -2.015332
5   1   NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN ... -1.000000   -1.000000   -1.000000   -1.000000   -1.000000   -1.000000   -1.000000   -1.000000   -1.000000   -1.000000
6   1   230.836429  509.991614  218.370072  2463.180957 526.564244  2231.603166 289.425584  298.146594  118.566019  754.736115  ... -0.807933   -1.509616   1.792957    10.396550   -1.060003   2.008286    1.029651    6.690478    -3.114476   0.766063
7   1   197.943186  355.930544  242.388461  2168.834937 412.196744  1753.647647 233.189894  241.823186  90.870574   512.000742  ... -1.630295   11.019253   -0.244958   2.188104    -0.505939   -0.564639   -1.747775   -0.394980   -2.736355   -0.140087
8   1   236.361903  491.867703  218.289537  2102.183175 657.764627  2792.688073 264.695685  249.063224  108.213277  662.192035  ... -1.655988   -1.555488   -1.199192   -0.565774   -1.831832   -4.770262   -0.442534   -6.168488   -0.267261   -3.324977
9   1   229.131335  372.101859  225.172708  2322.747894 333.243305  1800.901049 246.923254  287.262203  114.754666  562.854895  ... -2.419973   0.205031    -1.096847   -0.840121   -2.932670   1.719342    6.196965    -2.674245   -6.542936   -2.526353
10  1   208.748352  429.829772  222.081509  2095.421448 553.005620  2204.335371 259.718945  229.177512  102.475334  641.439810  ... 0.752312    -1.371583   -1.367145   -5.607321   3.259092    26.787332   -1.023199   -0.589042   0.507405    2.428903
11  1   248.233805  545.774276  241.743095  2390.945333 803.738236  3088.686081 277.757322  243.703551  131.933623  789.243830  ... -1.882445   -0.660089   -0.476966   -1.097497   -0.525270   -0.857579   -0.702017   0.016806    -0.792296   -0.368364
12  1   200.472858  353.177721  200.870312  2451.274841 295.858735  1556.379498 255.714673  301.000198  103.908244  514.528562  ... -0.789445   -14.382776  0.196276    -0.394203   7.600042    48.345830   -0.276618   -0.411825   2.271997    42.734886
12 rows × 601 columns

it has produced day 1 for each month instead of row which will show month 1 and day 1. What can i do to resolve this issue?

Remove one '' for one string:

df.query('month == 1 and day == 1')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM