如何从Pandas Group By获取另一个字段?

时间:2016-06-06 16:33:07

标签: python-3.x pandas

我有一个产生以下内容的数据框a

a = instruments['corn'].contracts().reset_index()

<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>Date</th>
      <th>Change</th>
      <th>Contract</th>
      <th>High</th>
      <th>Last</th>
      <th>Low</th>
      <th>Open</th>
      <th>Prev. Day Open Interest</th>
      <th>Settle</th>
      <th>Volatility</th>
      <th>Volume</th>
      <th>Expiration</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>1979-03-22</td>
      <td>0.0</td>
      <td>198005</td>
      <td>273.50</td>
      <td>0.0</td>
      <td>273.00</td>
      <td>273.50</td>
      <td>1390.0</td>
      <td>273.25</td>
      <td>0.000000</td>
      <td>1390.0</td>
      <td>1980-05-14</td>
    </tr>
    <tr>
      <th>1</th>
      <td>1979-03-23</td>
      <td>0.0</td>
      <td>198005</td>
      <td>273.25</td>
      <td>0.0</td>
      <td>272.50</td>
      <td>273.25</td>
      <td>1435.0</td>
      <td>272.50</td>
      <td>0.000000</td>
      <td>45.0</td>
      <td>1980-05-14</td>
    </tr>
    <tr>
      <th>2</th>
      <td>1979-03-26</td>
      <td>0.0</td>
      <td>198005</td>
      <td>274.50</td>
      <td>0.0</td>
      <td>273.25</td>
      <td>273.50</td>
      <td>1580.0</td>
      <td>274.50</td>
      <td>0.000000</td>
      <td>195.0</td>
      <td>1980-05-14</td>
    </tr>
    <tr>
      <th>3</th>
      <td>1979-03-27</td>
      <td>0.0</td>
      <td>198005</td>
      <td>274.50</td>
      <td>0.0</td>
      <td>273.50</td>
      <td>273.50</td>
      <td>1710.0</td>
      <td>274.50</td>
      <td>0.000000</td>
      <td>375.0</td>
      <td>1980-05-14</td>
    </tr>
    <tr>
      <th>4</th>
      <td>1979-03-28</td>
      <td>0.0</td>
      <td>198005</td>
      <td>277.75</td>
      <td>0.0</td>
      <td>275.00</td>
      <td>275.00</td>
      <td>2365.0</td>
      <td>277.75</td>
      <td>0.000000</td>
      <td>795.0</td>
      <td>1980-05-14</td>
    </tr>
    <tr>
      <th>5</th>
      <td>1979-03-29</td>
      <td>0.0</td>
      <td>198005</td>
      <td>279.00</td>
      <td>0.0</td>
      <td>277.00</td>
      <td>277.00</td>
      <td>3130.0</td>
      <td>277.00</td>
      <td>0.000000</td>
      <td>1890.0</td>
      <td>1980-05-14</td>
    </tr>
    <tr>
      <th>6</th>
      <td>1979-03-30</td>
      <td>0.0</td>
      <td>198005</td>
      <td>278.50</td>
      <td>0.0</td>
      <td>277.00</td>
      <td>277.00</td>
      <td>3550.0</td>
      <td>278.50</td>
      <td>0.000000</td>
      <td>620.0</td>
      <td>1980-05-14</td>
    </tr>
    <tr>
      <th>7</th>
      <td>1979-04-02</td>
      <td>0.0</td>
      <td>198005</td>
      <td>281.00</td>
      <td>0.0</td>
      <td>279.00</td>
      <td>279.00</td>
      <td>4550.0</td>
      <td>279.75</td>
      <td>0.000000</td>
      <td>1100.0</td>
      <td>1980-05-14</td>
    </tr>
    <tr>
      <th>8</th>
      <td>1979-04-03</td>
      <td>0.0</td>
      <td>198005</td>
      <td>279.50</td>
      <td>0.0</td>
      <td>277.25</td>
      <td>279.50</td>
      <td>4685.0</td>
      <td>278.25</td>
      <td>0.000000</td>
      <td>355.0</td>
      <td>1980-05-14</td>
    </tr>
    <tr>
      <th>9</th>
      <td>1979-04-04</td>
      <td>0.0</td>
      <td>198005</td>
      <td>278.75</td>
      <td>0.0</td>
      <td>278.00</td>
      <td>278.00</td>
      <td>5095.0</td>
      <td>278.50</td>
      <td>0.000000</td>
      <td>670.0</td>
      <td>1980-05-14</td>
    </tr>
    <tr>
      <th>10</th>
      <td>1979-04-05</td>
      <td>0.0</td>
      <td>198005</td>
      <td>279.25</td>
      <td>0.0</td>
      <td>278.50</td>
      <td>278.50</td>
      <td>5325.0</td>
      <td>279.00</td>
      <td>0.000000</td>
      <td>230.0</td>
      <td>1980-05-14</td>
    </tr>
    <tr>
      <th>11</th>
      <td>1979-04-06</td>
      <td>0.0</td>
      <td>198005</td>
      <td>280.00</td>
      <td>0.0</td>
      <td>278.00</td>
      <td>278.50</td>
      <td>5380.0</td>
      <td>280.00</td>
      <td>0.000000</td>
      <td>395.0</td>
      <td>1980-05-14</td>
    </tr>
    <tr>
      <th>12</th>
      <td>1979-04-09</td>
      <td>0.0</td>
      <td>198005</td>
      <td>281.50</td>
      <td>0.0</td>
      <td>279.50</td>
      <td>279.50</td>
      <td>5925.0</td>
      <td>280.50</td>
      <td>0.000000</td>
      <td>630.0</td>
      <td>1980-05-14</td>
    </tr>
    <tr>
      <th>13</th>
      <td>1979-04-10</td>
      <td>0.0</td>
      <td>198005</td>
      <td>280.00</td>
      <td>0.0</td>
      <td>278.75</td>
      <td>280.00</td>
      <td>6075.0</td>
      <td>280.00</td>
      <td>0.000000</td>
      <td>365.0</td>
      <td>1980-05-14</td>
    </tr>
    <tr>
      <th>14</th>
      <td>1979-04-11</td>
      <td>0.0</td>
      <td>198005</td>
      <td>281.00</td>
      <td>0.0</td>
      <td>279.00</td>
      <td>280.00</td>
      <td>5755.0</td>
      <td>280.50</td>
      <td>0.000000</td>
      <td>1520.0</td>
      <td>1980-05-14</td>
    </tr>
    <tr>
      <th>15</th>
      <td>1979-04-12</td>
      <td>0.0</td>
      <td>198005</td>
      <td>280.25</td>
      <td>0.0</td>
      <td>278.25</td>
      <td>280.25</td>
      <td>5735.0</td>
      <td>278.25</td>
      <td>0.000000</td>
      <td>320.0</td>
      <td>1980-05-14</td>
    </tr>
    <tr>
      <th>16</th>
      <td>1979-04-16</td>
      <td>0.0</td>
      <td>198005</td>
      <td>277.25</td>
      <td>0.0</td>
      <td>275.75</td>
      <td>276.00</td>
      <td>6110.0</td>
      <td>276.50</td>
      <td>0.000000</td>
      <td>860.0</td>
      <td>1980-05-14</td>
    </tr>
    <tr>
      <th>17</th>
      <td>1979-04-17</td>
      <td>0.0</td>
      <td>198005</td>
      <td>278.75</td>
      <td>0.0</td>
      <td>276.75</td>
      <td>276.75</td>
      <td>6555.0</td>
      <td>278.75</td>
      <td>0.000000</td>
      <td>965.0</td>
      <td>1980-05-14</td>
    </tr>
    <tr>
      <th>18</th>
      <td>1979-04-18</td>
      <td>0.0</td>
      <td>198005</td>
      <td>278.50</td>
      <td>0.0</td>
      <td>277.25</td>
      <td>278.00</td>
      <td>6630.0</td>
      <td>278.00</td>
      <td>0.000000</td>
      <td>120.0</td>
      <td>1980-05-14</td>
    </tr>
    <tr>
      <th>19</th>
      <td>1979-04-19</td>
      <td>0.0</td>
      <td>198005</td>
      <td>278.00</td>
      <td>0.0</td>
      <td>276.25</td>
      <td>277.25</td>
      <td>6795.0</td>
      <td>277.00</td>
      <td>0.000000</td>
      <td>1315.0</td>
      <td>1980-05-14</td>
    </tr>
    <tr>
      <th>20</th>
      <td>1979-04-20</td>
      <td>0.0</td>
      <td>198005</td>
      <td>277.00</td>
      <td>0.0</td>
      <td>275.75</td>
      <td>276.25</td>
      <td>6910.0</td>
      <td>277.00</td>
      <td>0.000000</td>
      <td>255.0</td>
      <td>1980-05-14</td>
    </tr>
    <tr>
      <th>21</th>
      <td>1979-04-23</td>
      <td>0.0</td>
      <td>198005</td>
      <td>278.50</td>
      <td>0.0</td>
      <td>276.25</td>
      <td>276.25</td>
      <td>8580.0</td>
      <td>278.50</td>
      <td>0.000000</td>
      <td>2290.0</td>
      <td>1980-05-14</td>
    </tr>
    <tr>
      <th>22</th>
      <td>1979-04-24</td>
      <td>0.0</td>
      <td>198005</td>
      <td>282.00</td>
      <td>0.0</td>
      <td>279.00</td>
      <td>279.50</td>
      <td>9160.0</td>
      <td>282.00</td>
      <td>0.000000</td>
      <td>790.0</td>
      <td>1980-05-14</td>
    </tr>
    <tr>
      <th>23</th>
      <td>1979-04-25</td>
      <td>0.0</td>
      <td>198005</td>
      <td>282.50</td>
      <td>0.0</td>
      <td>280.50</td>
      <td>280.75</td>
      <td>10500.0</td>
      <td>281.25</td>
      <td>0.000000</td>
      <td>1870.0</td>
      <td>1980-05-14</td>
    </tr>
    <tr>
      <th>24</th>
      <td>1979-04-26</td>
      <td>0.0</td>
      <td>198005</td>
      <td>283.00</td>
      <td>0.0</td>
      <td>279.75</td>
      <td>280.75</td>
      <td>10780.0</td>
      <td>282.00</td>
      <td>0.000000</td>
      <td>1950.0</td>
      <td>1980-05-14</td>
    </tr>
    <tr>
      <th>25</th>
      <td>1979-04-27</td>
      <td>0.0</td>
      <td>198005</td>
      <td>284.75</td>
      <td>0.0</td>
      <td>282.25</td>
      <td>283.00</td>
      <td>10485.0</td>
      <td>282.25</td>
      <td>0.000000</td>
      <td>1880.0</td>
      <td>1980-05-14</td>
    </tr>
    <tr>
      <th>26</th>
      <td>1979-04-30</td>
      <td>0.0</td>
      <td>198005</td>
      <td>287.25</td>
      <td>0.0</td>
      <td>282.50</td>
      <td>282.75</td>
      <td>11765.0</td>
      <td>287.25</td>
      <td>0.000000</td>
      <td>5070.0</td>
      <td>1980-05-14</td>
    </tr>
    <tr>
      <th>27</th>
      <td>1979-05-01</td>
      <td>0.0</td>
      <td>198005</td>
      <td>289.00</td>
      <td>0.0</td>
      <td>286.50</td>
      <td>287.00</td>
      <td>13000.0</td>
      <td>288.25</td>
      <td>0.000000</td>
      <td>2360.0</td>
      <td>1980-05-14</td>
    </tr>
    <tr>
      <th>28</th>
      <td>1979-05-02</td>
      <td>0.0</td>
      <td>198005</td>
      <td>295.00</td>
      <td>0.0</td>
      <td>289.00</td>
      <td>289.00</td>
      <td>14730.0</td>
      <td>290.25</td>
      <td>0.000000</td>
      <td>3200.0</td>
      <td>1980-05-14</td>
    </tr>
    <tr>
      <th>29</th>
      <td>1979-05-03</td>
      <td>0.0</td>
      <td>198005</td>
      <td>291.75</td>
      <td>0.0</td>
      <td>285.00</td>
      <td>291.75</td>
      <td>15100.0</td>
      <td>287.00</td>
      <td>0.000000</td>
      <td>1680.0</td>
      <td>1980-05-14</td>
    </tr>
    <tr>
      <th>...</th>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
    </tr>
    <tr>
      <th>115809</th>
      <td>1990-08-08</td>
      <td>0.0</td>
      <td>199009</td>
      <td>253.75</td>
      <td>0.0</td>
      <td>251.75</td>
      <td>253.75</td>
      <td>161630.0</td>
      <td>252.25</td>
      <td>0.015012</td>
      <td>38160.0</td>
      <td>1990-09-14</td>
    </tr>
    <tr>
      <th>115810</th>
      <td>1990-08-09</td>
      <td>0.0</td>
      <td>199009</td>
      <td>252.25</td>
      <td>0.0</td>
      <td>250.00</td>
      <td>251.50</td>
      <td>159955.0</td>
      <td>250.25</td>
      <td>0.014661</td>
      <td>30105.0</td>
      <td>1990-09-14</td>
    </tr>
    <tr>
      <th>115811</th>
      <td>1990-08-10</td>
      <td>0.0</td>
      <td>199009</td>
      <td>254.00</td>
      <td>0.0</td>
      <td>249.75</td>
      <td>252.50</td>
      <td>159195.0</td>
      <td>250.25</td>
      <td>0.014339</td>
      <td>39585.0</td>
      <td>1990-09-14</td>
    </tr>
    <tr>
      <th>115812</th>
      <td>1990-08-13</td>
      <td>0.0</td>
      <td>199009</td>
      <td>249.50</td>
      <td>0.0</td>
      <td>244.00</td>
      <td>249.50</td>
      <td>158425.0</td>
      <td>245.25</td>
      <td>0.014102</td>
      <td>43025.0</td>
      <td>1990-09-14</td>
    </tr>
    <tr>
      <th>115813</th>
      <td>1990-08-14</td>
      <td>0.0</td>
      <td>199009</td>
      <td>245.50</td>
      <td>0.0</td>
      <td>243.75</td>
      <td>244.25</td>
      <td>155785.0</td>
      <td>245.50</td>
      <td>0.014326</td>
      <td>40915.0</td>
      <td>1990-09-14</td>
    </tr>
    <tr>
      <th>115814</th>
      <td>1990-08-15</td>
      <td>0.0</td>
      <td>199009</td>
      <td>247.75</td>
      <td>0.0</td>
      <td>244.50</td>
      <td>246.25</td>
      <td>154925.0</td>
      <td>247.50</td>
      <td>0.014211</td>
      <td>36445.0</td>
      <td>1990-09-14</td>
    </tr>
    <tr>
      <th>115815</th>
      <td>1990-08-16</td>
      <td>0.0</td>
      <td>199009</td>
      <td>250.00</td>
      <td>0.0</td>
      <td>247.50</td>
      <td>248.00</td>
      <td>153660.0</td>
      <td>249.00</td>
      <td>0.014025</td>
      <td>44395.0</td>
      <td>1990-09-14</td>
    </tr>
    <tr>
      <th>115816</th>
      <td>1990-08-17</td>
      <td>0.0</td>
      <td>199009</td>
      <td>254.00</td>
      <td>0.0</td>
      <td>247.75</td>
      <td>248.75</td>
      <td>152610.0</td>
      <td>250.75</td>
      <td>0.013697</td>
      <td>49635.0</td>
      <td>1990-09-14</td>
    </tr>
    <tr>
      <th>115817</th>
      <td>1990-08-20</td>
      <td>0.0</td>
      <td>199009</td>
      <td>254.00</td>
      <td>0.0</td>
      <td>251.00</td>
      <td>251.00</td>
      <td>146950.0</td>
      <td>252.25</td>
      <td>0.013579</td>
      <td>36695.0</td>
      <td>1990-09-14</td>
    </tr>
    <tr>
      <th>115818</th>
      <td>1990-08-21</td>
      <td>0.0</td>
      <td>199009</td>
      <td>253.75</td>
      <td>0.0</td>
      <td>250.25</td>
      <td>252.00</td>
      <td>144820.0</td>
      <td>251.75</td>
      <td>0.013266</td>
      <td>51935.0</td>
      <td>1990-09-14</td>
    </tr>
    <tr>
      <th>115819</th>
      <td>1990-08-22</td>
      <td>0.0</td>
      <td>199009</td>
      <td>254.75</td>
      <td>0.0</td>
      <td>251.50</td>
      <td>252.50</td>
      <td>141515.0</td>
      <td>253.75</td>
      <td>0.012918</td>
      <td>50210.0</td>
      <td>1990-09-14</td>
    </tr>
    <tr>
      <th>115820</th>
      <td>1990-08-23</td>
      <td>0.0</td>
      <td>199009</td>
      <td>260.00</td>
      <td>0.0</td>
      <td>255.00</td>
      <td>255.00</td>
      <td>138385.0</td>
      <td>259.00</td>
      <td>0.012822</td>
      <td>50155.0</td>
      <td>1990-09-14</td>
    </tr>
    <tr>
      <th>115821</th>
      <td>1990-08-24</td>
      <td>0.0</td>
      <td>199009</td>
      <td>260.00</td>
      <td>0.0</td>
      <td>257.25</td>
      <td>258.00</td>
      <td>121575.0</td>
      <td>257.75</td>
      <td>0.012794</td>
      <td>38135.0</td>
      <td>1990-09-14</td>
    </tr>
    <tr>
      <th>115822</th>
      <td>1990-08-27</td>
      <td>0.0</td>
      <td>199009</td>
      <td>256.50</td>
      <td>0.0</td>
      <td>251.50</td>
      <td>256.00</td>
      <td>112135.0</td>
      <td>252.00</td>
      <td>0.012563</td>
      <td>34850.0</td>
      <td>1990-09-14</td>
    </tr>
    <tr>
      <th>115823</th>
      <td>1990-08-28</td>
      <td>0.0</td>
      <td>199009</td>
      <td>254.00</td>
      <td>0.0</td>
      <td>251.00</td>
      <td>251.00</td>
      <td>104110.0</td>
      <td>253.75</td>
      <td>0.012990</td>
      <td>44680.0</td>
      <td>1990-09-14</td>
    </tr>
    <tr>
      <th>115824</th>
      <td>1990-08-29</td>
      <td>0.0</td>
      <td>199009</td>
      <td>255.50</td>
      <td>0.0</td>
      <td>249.75</td>
      <td>254.75</td>
      <td>91055.0</td>
      <td>250.00</td>
      <td>0.013190</td>
      <td>64930.0</td>
      <td>1990-09-14</td>
    </tr>
    <tr>
      <th>115825</th>
      <td>1990-08-30</td>
      <td>0.0</td>
      <td>199009</td>
      <td>249.50</td>
      <td>0.0</td>
      <td>245.00</td>
      <td>249.25</td>
      <td>77335.0</td>
      <td>245.25</td>
      <td>0.013716</td>
      <td>73035.0</td>
      <td>1990-09-14</td>
    </tr>
    <tr>
      <th>115826</th>
      <td>1990-08-31</td>
      <td>0.0</td>
      <td>199009</td>
      <td>248.00</td>
      <td>0.0</td>
      <td>241.50</td>
      <td>246.00</td>
      <td>65310.0</td>
      <td>243.25</td>
      <td>0.013584</td>
      <td>53775.0</td>
      <td>1990-09-14</td>
    </tr>
    <tr>
      <th>115827</th>
      <td>1990-09-04</td>
      <td>0.0</td>
      <td>199009</td>
      <td>243.00</td>
      <td>0.0</td>
      <td>239.50</td>
      <td>243.00</td>
      <td>56660.0</td>
      <td>240.75</td>
      <td>0.013397</td>
      <td>30185.0</td>
      <td>1990-09-14</td>
    </tr>
    <tr>
      <th>115828</th>
      <td>1990-09-05</td>
      <td>0.0</td>
      <td>199009</td>
      <td>243.75</td>
      <td>0.0</td>
      <td>241.50</td>
      <td>241.75</td>
      <td>52140.0</td>
      <td>243.25</td>
      <td>0.013030</td>
      <td>23890.0</td>
      <td>1990-09-14</td>
    </tr>
    <tr>
      <th>115829</th>
      <td>1990-09-06</td>
      <td>0.0</td>
      <td>199009</td>
      <td>244.50</td>
      <td>0.0</td>
      <td>241.75</td>
      <td>244.50</td>
      <td>42070.0</td>
      <td>243.00</td>
      <td>0.013099</td>
      <td>24200.0</td>
      <td>1990-09-14</td>
    </tr>
    <tr>
      <th>115830</th>
      <td>1990-09-07</td>
      <td>0.0</td>
      <td>199009</td>
      <td>242.00</td>
      <td>0.0</td>
      <td>238.50</td>
      <td>242.00</td>
      <td>34570.0</td>
      <td>238.75</td>
      <td>0.012864</td>
      <td>29765.0</td>
      <td>1990-09-14</td>
    </tr>
    <tr>
      <th>115831</th>
      <td>1990-09-10</td>
      <td>0.0</td>
      <td>199009</td>
      <td>240.50</td>
      <td>0.0</td>
      <td>235.00</td>
      <td>237.00</td>
      <td>31165.0</td>
      <td>239.50</td>
      <td>0.013181</td>
      <td>8510.0</td>
      <td>1990-09-14</td>
    </tr>
    <tr>
      <th>115832</th>
      <td>1990-09-11</td>
      <td>0.0</td>
      <td>199009</td>
      <td>241.50</td>
      <td>0.0</td>
      <td>239.00</td>
      <td>240.00</td>
      <td>28485.0</td>
      <td>241.50</td>
      <td>0.013361</td>
      <td>6850.0</td>
      <td>1990-09-14</td>
    </tr>
    <tr>
      <th>115833</th>
      <td>1990-09-12</td>
      <td>0.0</td>
      <td>199009</td>
      <td>241.50</td>
      <td>0.0</td>
      <td>238.75</td>
      <td>240.75</td>
      <td>25310.0</td>
      <td>239.25</td>
      <td>0.013057</td>
      <td>7865.0</td>
      <td>1990-09-14</td>
    </tr>
    <tr>
      <th>115834</th>
      <td>1990-09-13</td>
      <td>0.0</td>
      <td>199009</td>
      <td>234.00</td>
      <td>0.0</td>
      <td>231.75</td>
      <td>233.00</td>
      <td>21410.0</td>
      <td>233.25</td>
      <td>0.014449</td>
      <td>8975.0</td>
      <td>1990-09-14</td>
    </tr>
    <tr>
      <th>115835</th>
      <td>1990-09-14</td>
      <td>0.0</td>
      <td>199009</td>
      <td>233.50</td>
      <td>0.0</td>
      <td>231.75</td>
      <td>233.00</td>
      <td>19690.0</td>
      <td>232.75</td>
      <td>0.014074</td>
      <td>4505.0</td>
      <td>1990-09-14</td>
    </tr>
    <tr>
      <th>115836</th>
      <td>1990-09-17</td>
      <td>0.0</td>
      <td>199009</td>
      <td>235.75</td>
      <td>0.0</td>
      <td>232.25</td>
      <td>233.75</td>
      <td>16135.0</td>
      <td>235.75</td>
      <td>0.013777</td>
      <td>9515.0</td>
      <td>1990-09-14</td>
    </tr>
    <tr>
      <th>115837</th>
      <td>1990-09-18</td>
      <td>0.0</td>
      <td>199009</td>
      <td>237.75</td>
      <td>0.0</td>
      <td>236.00</td>
      <td>236.25</td>
      <td>8635.0</td>
      <td>237.00</td>
      <td>0.013788</td>
      <td>11545.0</td>
      <td>1990-09-14</td>
    </tr>
    <tr>
      <th>115838</th>
      <td>1990-09-19</td>
      <td>0.0</td>
      <td>199009</td>
      <td>237.00</td>
      <td>0.0</td>
      <td>232.50</td>
      <td>237.00</td>
      <td>980.0</td>
      <td>234.25</td>
      <td>0.013469</td>
      <td>10070.0</td>
      <td>1990-09-14</td>
    </tr>
  </tbody>
</table>

对于每一天,我都试图获得最高音量的合约字段。

我差不多了:

a.groupby(['Date'],as_index=False).agg({'Volume':np.max})

生成一个包含天数和最高卷的数据框,但我无法弄清楚如何获得相应的合同,因为它没有映射到原始(非分组)数据框的索引。

如何获得相应的合约字段?

<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>Date</th>
      <th>Volume</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>1959-07-01</td>
      <td>541.0</td>
    </tr>
    <tr>
      <th>1</th>
      <td>1959-07-02</td>
      <td>1156.0</td>
    </tr>
    <tr>
      <th>2</th>
      <td>1959-07-06</td>
      <td>1495.0</td>
    </tr>
    <tr>
      <th>3</th>
      <td>1959-07-07</td>
      <td>1204.0</td>
    </tr>
    <tr>
      <th>4</th>
      <td>1959-07-08</td>
      <td>1244.0</td>
    </tr>
    <tr>
      <th>5</th>
      <td>1959-07-09</td>
      <td>921.0</td>
    </tr>
    <tr>
      <th>6</th>
      <td>1959-07-10</td>
      <td>1473.0</td>
    </tr>
    <tr>
      <th>7</th>
      <td>1959-07-13</td>
      <td>1457.0</td>
    </tr>
    <tr>
      <th>8</th>
      <td>1959-07-14</td>
      <td>1511.0</td>
    </tr>
    <tr>
      <th>9</th>
      <td>1959-07-15</td>
      <td>1778.0</td>
    </tr>
    <tr>
      <th>10</th>
      <td>1959-07-16</td>
      <td>1057.0</td>
    </tr>
    <tr>
      <th>11</th>
      <td>1959-07-17</td>
      <td>671.0</td>
    </tr>
    <tr>
      <th>12</th>
      <td>1959-07-20</td>
      <td>868.0</td>
    </tr>
    <tr>
      <th>13</th>
      <td>1959-07-21</td>
      <td>1539.0</td>
    </tr>
    <tr>
      <th>14</th>
      <td>1959-07-22</td>
      <td>1089.0</td>
    </tr>
    <tr>
      <th>15</th>
      <td>1959-07-23</td>
      <td>924.0</td>
    </tr>
    <tr>
      <th>16</th>
      <td>1959-07-24</td>
      <td>1399.0</td>
    </tr>
    <tr>
      <th>17</th>
      <td>1959-07-27</td>
      <td>680.0</td>
    </tr>
    <tr>
      <th>18</th>
      <td>1959-07-28</td>
      <td>664.0</td>
    </tr>
    <tr>
      <th>19</th>
      <td>1959-07-29</td>
      <td>744.0</td>
    </tr>
    <tr>
      <th>20</th>
      <td>1959-07-30</td>
      <td>599.0</td>
    </tr>
    <tr>
      <th>21</th>
      <td>1959-07-31</td>
      <td>1030.0</td>
    </tr>
    <tr>
      <th>22</th>
      <td>1959-08-03</td>
      <td>623.0</td>
    </tr>
    <tr>
      <th>23</th>
      <td>1959-08-04</td>
      <td>898.0</td>
    </tr>
    <tr>
      <th>24</th>
      <td>1959-08-05</td>
      <td>1006.0</td>
    </tr>
    <tr>
      <th>25</th>
      <td>1959-08-06</td>
      <td>495.0</td>
    </tr>
    <tr>
      <th>26</th>
      <td>1959-08-07</td>
      <td>1851.0</td>
    </tr>
    <tr>
      <th>27</th>
      <td>1959-08-10</td>
      <td>1229.0</td>
    </tr>
    <tr>
      <th>28</th>
      <td>1959-08-11</td>
      <td>909.0</td>
    </tr>
    <tr>
      <th>29</th>
      <td>1959-08-12</td>
      <td>535.0</td>
    </tr>
    <tr>
      <th>...</th>
      <td>...</td>
      <td>...</td>
    </tr>
    <tr>
      <th>14304</th>
      <td>2016-04-14</td>
      <td>277581.0</td>
    </tr>
    <tr>
      <th>14305</th>
      <td>2016-04-15</td>
      <td>281613.0</td>
    </tr>
  </tbody>
</table>

1 个答案:

答案 0 :(得分:0)

我认为您可以首先通过汇总idxmax找到具有最大值的列Volume的索引,然后按loc选择df

df.loc[df.groupby("Date")["Volume"].idxmax()]

如果需要Contract

print (df.loc[df.groupby("Date")["Volume"].idxmax(), 'Contract'])

另一个解决方案首先是Volumedf.sort_values(by="Volume", ascending=False).groupby("Date", as_index=False).first() ,然后是sort_values,汇总groupby

import pandas as pd

df = pd.DataFrame({'Prev. Day Open Interest': {115809: 161630.0, 115813: 155785.0, 115814: 154925.0, 115815: 153660.0, 115823: 104110.0, 115824: 91055.0, 115825: 77335.0, 115826: 65310.0, 115827: 56660.0, 115828: 52140.0, 115835: 19690.0, 115838: 980.0}, 'Date': {115809: '1990-08-08', 115813: '1990-08-08', 115814: '1990-08-15', 115815: '1990-08-15', 115823: '1990-08-23', 115824: '1990-08-23', 115825: '1990-08-30', 115826: '1990-08-30', 115827: '1990-09-04', 115828: '1990-09-04', 115835: '1990-09-11', 115838: '1990-09-11'}, 'Settle': {115809: 252.25, 115813: 245.5, 115814: 247.5, 115815: 249.0, 115823: 253.75, 115824: 250.0, 115825: 245.25, 115826: 243.25, 115827: 240.75, 115828: 243.25, 115835: 232.75, 115838: 234.25}, 'Expiration': {115809: '1990-09-14', 115813: '1990-09-14', 115814: '1990-09-14', 115815: '1990-09-14', 115823: '1990-09-14', 115824: '1990-09-14', 115825: '1990-09-14', 115826: '1990-09-14', 115827: '1990-09-14', 115828: '1990-09-14', 115835: '1990-09-14', 115838: '1990-09-14'}, 'Volume': {115809: 38160.0, 115813: 40915.0, 115814: 36445.0, 115815: 44395.0, 115823: 44680.0, 115824: 64930.0, 115825: 73035.0, 115826: 53775.0, 115827: 30185.0, 115828: 23890.0, 115835: 4505.0, 115838: 10070.0}, 'Volatility': {115809: 0.015012000000000001, 115813: 0.014325999999999998, 115814: 0.014211000000000001, 115815: 0.014024999999999999, 115823: 0.01299, 115824: 0.01319, 115825: 0.013715999999999999, 115826: 0.013584000000000001, 115827: 0.013396999999999999, 115828: 0.013030000000000002, 115835: 0.014074000000000001, 115838: 0.013469}, 'Last': {115809: 0.0, 115813: 0.0, 115814: 0.0, 115815: 0.0, 115823: 0.0, 115824: 0.0, 115825: 0.0, 115826: 0.0, 115827: 0.0, 115828: 0.0, 115835: 0.0, 115838: 0.0}, 'High': {115809: 253.75, 115813: 245.5, 115814: 247.75, 115815: 250.0, 115823: 254.0, 115824: 255.5, 115825: 249.5, 115826: 248.0, 115827: 243.0, 115828: 243.75, 115835: 233.5, 115838: 237.0}, 'Low': {115809: 251.75, 115813: 243.75, 115814: 244.5, 115815: 247.5, 115823: 251.0, 115824: 249.75, 115825: 245.0, 115826: 241.5, 115827: 239.5, 115828: 241.5, 115835: 231.75, 115838: 232.5}, 'Contract': {115809: 199001, 115813: 199002, 115814: 199003, 115815: 199004, 115823: 199005, 115824: 199006, 115825: 199007, 115826: 199008, 115827: 199009, 115828: 199010, 115835: 199011, 115838: 199012}, 'Open': {115809: 253.75, 115813: 244.25, 115814: 246.25, 115815: 248.0, 115823: 251.0, 115824: 254.75, 115825: 249.25, 115826: 246.0, 115827: 243.0, 115828: 241.75, 115835: 233.0, 115838: 237.0}, 'Change': {115809: 0.0, 115813: 0.0, 115814: 0.0, 115815: 0.0, 115823: 0.0, 115824: 0.0, 115825: 0.0, 115826: 0.0, 115827: 0.0, 115828: 0.0, 115835: 0.0, 115838: 0.0}})
print (df)
        Change  Contract        Date  Expiration    High  Last     Low    Open  Prev. Day Open Interest  Settle  Volatility   Volume
115809     0.0    199001  1990-08-08  1990-09-14  253.75   0.0  251.75  253.75                 161630.0  252.25    0.015012  38160.0
115813     0.0    199002  1990-08-08  1990-09-14  245.50   0.0  243.75  244.25                 155785.0  245.50    0.014326  40915.0
115814     0.0    199003  1990-08-15  1990-09-14  247.75   0.0  244.50  246.25                 154925.0  247.50    0.014211  36445.0
115815     0.0    199004  1990-08-15  1990-09-14  250.00   0.0  247.50  248.00                 153660.0  249.00    0.014025  44395.0
115823     0.0    199005  1990-08-23  1990-09-14  254.00   0.0  251.00  251.00                 104110.0  253.75    0.012990  44680.0
115824     0.0    199006  1990-08-23  1990-09-14  255.50   0.0  249.75  254.75                  91055.0  250.00    0.013190  64930.0
115825     0.0    199007  1990-08-30  1990-09-14  249.50   0.0  245.00  249.25                  77335.0  245.25    0.013716  73035.0
115826     0.0    199008  1990-08-30  1990-09-14  248.00   0.0  241.50  246.00                  65310.0  243.25    0.013584  53775.0
115827     0.0    199009  1990-09-04  1990-09-14  243.00   0.0  239.50  243.00                  56660.0  240.75    0.013397  30185.0
115828     0.0    199010  1990-09-04  1990-09-14  243.75   0.0  241.50  241.75                  52140.0  243.25    0.013030  23890.0
115835     0.0    199011  1990-09-11  1990-09-14  233.50   0.0  231.75  233.00                  19690.0  232.75    0.014074   4505.0
115838     0.0    199012  1990-09-11  1990-09-14  237.00   0.0  232.50  237.00                    980.0  234.25    0.013469  10070.0
print (df.loc[df.groupby("Date")["Volume"].idxmax()])
        Change  Contract        Date  Expiration   High  Last     Low    Open  Prev. Day Open Interest  Settle  Volatility   Volume
115813     0.0    199002  1990-08-08  1990-09-14  245.5   0.0  243.75  244.25                 155785.0  245.50    0.014326  40915.0
115815     0.0    199004  1990-08-15  1990-09-14  250.0   0.0  247.50  248.00                 153660.0  249.00    0.014025  44395.0
115824     0.0    199006  1990-08-23  1990-09-14  255.5   0.0  249.75  254.75                  91055.0  250.00    0.013190  64930.0
115825     0.0    199007  1990-08-30  1990-09-14  249.5   0.0  245.00  249.25                  77335.0  245.25    0.013716  73035.0
115827     0.0    199009  1990-09-04  1990-09-14  243.0   0.0  239.50  243.00                  56660.0  240.75    0.013397  30185.0
115838     0.0    199012  1990-09-11  1990-09-14  237.0   0.0  232.50  237.00                    980.0  234.25    0.013469  10070.0

print (df.sort_values(by="Volume", ascending=False).groupby("Date", as_index=False).first())
         Date  Change  Contract  Expiration   High  Last     Low    Open  Prev. Day Open Interest  Settle  Volatility   Volume
0  1990-08-08     0.0    199002  1990-09-14  245.5   0.0  243.75  244.25                 155785.0  245.50    0.014326  40915.0
1  1990-08-15     0.0    199004  1990-09-14  250.0   0.0  247.50  248.00                 153660.0  249.00    0.014025  44395.0
2  1990-08-23     0.0    199006  1990-09-14  255.5   0.0  249.75  254.75                  91055.0  250.00    0.013190  64930.0
3  1990-08-30     0.0    199007  1990-09-14  249.5   0.0  245.00  249.25                  77335.0  245.25    0.013716  73035.0
4  1990-09-04     0.0    199009  1990-09-14  243.0   0.0  239.50  243.00                  56660.0  240.75    0.013397  30185.0
5  1990-09-11     0.0    199012  1990-09-14  237.0   0.0  232.50  237.00                    980.0  234.25    0.013469  10070.0

示例(某些数据已更改):

print (df.loc[df.groupby("Date")["Volume"].idxmax(), 'Contract'])
115813    199002
115815    199004
115824    199006
115825    199007
115827    199009
115838    199012
Name: Contract, dtype: int64

print (df.sort_values(by="Volume", ascending=False).groupby("Date", as_index=False).first()['Contract'])
0    199002
1    199004
2    199006
3    199007
4    199009
5    199012
Name: Contract, dtype: int64
<?php
    $products;//What ever you have assigned to this...

    $size = count($products); // size of products array
    $tmp = PHP_INT_MAX; // Set this to the highest possible counter value.

    for($i = 0; $i < $size; $i++)
    {
        //If a new lowest counter has been found empty array and append this value
        if($products[$i]["counter"] < $tmp)
        {
            $lowest = array();
            $lowest[0] = $products[$i]["item"];
            $tmp = $products[$i]["counter"];
        }

        //If counter is equally low append value
        if($products[$i]["counter"] == $tmp)
        {
            $lowest[] = $products[$i]["item"];
        }
    }

    echo array_rand($lowest);
?>