Integer slicing in pandas different for rows and columns?
Question: Coming from R I try to get my head around integer slicing for pandas
dataframes. What puzzles me is the different slicing behavior for rows and
columns using the same integer/slice expression.

    
    
    import pandas as pd
    
    x = pd.DataFrame({'a': range(0,6),
                   'b': range(7,13),
                   'c': range(14, 20)})
    x.ix[0:2, 0:2]   # Why 3 x 2 and not 3 x 3 or 2 x 2?
    
        a   b
    0   0   7
    1   1   8
    2   2   9
    

We get 3 rows but only 2 columns. In the docs I find that different from
standard python, [label based slicing in pandas is
inclusive](http://pandas.pydata.org/pandas-docs/stable/gotchas.html#endpoints-
are-inclusive). Does this apply here and is it inclusive for rows but not for
columns then?

**Can someone explain the behavior and the rationale behind it?**


Answer: You are correct that there is a distinction between **label based indexing**
and **position based indexing**. The first includes the end label, while
typical python position based slicing does not include the last item.

In the example you give: `x.ix[0:2, 0:2]` the rows are being sliced based on
the labels, so '2' is included (returning 3 rows), while the columns are
sliced based on position, hence returning only 2 columns.

If you want guaranteed position based slicing (to return a 2x2 frame in this
case), `iloc` is the indexer to use:

    
    
    In [6]: x.iloc[0:2, 0:2]
    Out[6]:
       a  b
    0  0  7
    1  1  8
    

For guaranteed position based slicing, you can use the `loc` indexer.  
The `ix` indexer you are using, is more flexible (not strict in type of
indexing). It is primarily label based, but will fall back to position based
(when the labels are not found and you are using integers). This is the case
in your example for the columns. For this reason, it is **recommended to
always use`loc`/`iloc` instead of `ix`** (unless you need mixed label/position
based indexing).

See the docs for a more detailed overview of the different types of indexers:
<http://pandas.pydata.org/pandas-docs/stable/indexing.html#different-choices-
for-indexing>

