pandas Indexing and selecting data Mixed position and label based selection


Example

DataFrame:

import pandas as pd
import numpy as np
np.random.seed(5)
df = pd.DataFrame(np.random.randint(100, size=(5, 5)), columns = list("ABCDE"), 
                  index = ["R" + str(i) for i in range(5)])


df
Out[12]: 
     A   B   C   D   E
R0  99  78  61  16  73
R1   8  62  27  30  80
R2   7  76  15  53  80
R3  27  44  77  75  65
R4  47  30  84  86  18

Select rows by position, and columns by label:

df.ix[1:3, 'C':'E']
Out[19]: 
     C   D   E
R1   5  58   0
R2  36  51  27

If the index is integer, .ix will use labels rather than positions:

df.index = np.arange(5, 10)

df
Out[22]: 
    A   B   C   D   E
5   9  41  62   1  82
6  16  78   5  58   0
7  80   4  36  51  27
8  31   2  68  38  83
9  19  18   7  30  62

#same call returns an empty DataFrame because now the index is integer
df.ix[1:3, 'C':'E']
Out[24]: 
Empty DataFrame
Columns: [C, D, E]
Index: []