Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As <a href="https://stackoverflow.com/a/31490891/1240268">Robbie-Clarken answers</a>, since 0.14 you can pass a <a href="http://pandas.pydata.org/pandas-docs/stable/advanced.html#using-slicers" rel="noreferrer">slice in the tuple you pass to loc</a>:</p> <pre><code>In [11]: s.loc[('b', slice(2, 10))] Out[11]: b 2 -0.65394 4 0.08227 dtype: float64 </code></pre> <p>Indeed, you can pass a slice for each level:</p> <pre><code>In [12]: s.loc[(slice('a', 'b'), slice(2, 10))] Out[12]: a 5 0.27919 b 2 -0.65394 4 0.08227 dtype: float64 </code></pre> <p><em>Note: the slice is inclusive.</em></p> <hr> <h3>Old answer:</h3> <p>You can also do this using:</p> <pre><code>s.ix[1:10, "b"] </code></pre> <p>(It's good practice to do in a single ix/loc/iloc since this version allows assignment.)</p> <p>This answer was written prior to the <a href="https://github.com/pydata/pandas/pull/2922" rel="noreferrer">introduction of iloc</a> in early 2013, i.e. position/integer location - which may be preferred in this case. The reason it was created was to remove the ambiguity from integer-indexed pandas objects, and be more descriptive: "I'm slicing on position".</p> <pre><code>s["b"].iloc[1:10] </code></pre> <p>That said, I kinda disagree with the docs that ix is:</p> <blockquote> <p>most robust and consistent way</p> </blockquote> <p>it's not, the most consistent way is to describe what you're doing:</p> <ul> <li>use loc for labels</li> <li>use iloc for position</li> <li>use ix for both (if you really have to)</li> </ul> <p>Remember the <a href="https://www.python.org/dev/peps/pep-0020/" rel="noreferrer">zen of python</a>:</p> <blockquote> <p>explicit is better than implicit</p> </blockquote>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload