Pandas - correct indexing by position on the integer axis and label on the other (to avoid binding to the chain)

I know pandas provides:

  • .ix - mixed label and position indexing (primarily the label), if the index is an integer - it will be interpreted as a label
  • .loc - explicit indexing by tag
  • .iloc - explicit indexing by position

That's cool.

What would be the correct way to index columns by label (ala .loc) and rows by position (ala .iloc) in one shot to avoid chaining? Preferably to avoid reset_index().

To provide an example, suppose the following DataFrame df:

   col1  col2  col3
3     1     4     2
2     2     3     3
4     3     2     1
1     4     1     4

Does pandas have something like some_indexerthat that behaves as follows:

In[2]: df.some_indexer[2,'col2':'col3']
Out[2]:
col2    2
col3    1
Name: 4, dtype: object

Thank!

+4
2

, , reset_index(). df.index[2] .loc.

df.loc[df.index[2],'col2':'col3']

.

+2

df.ix[2, 'col2':'col3'], , , , 2 1, , ix , .

:

.ix . , , ..ix .loc .iloc..ix ..ix - .

In [246]:

df.ix[2,'col2':'col3']
Out[246]:
col2    3
col3    3
Name: 2, dtype: int64

, , , :

In [247]:

df.iloc[2][['col2','col3']]
Out[247]:
col2    2
col3    1
Name: 4, dtype: int64

reset , ix, , :

In [250]:

df = df.reset_index().drop('index',axis=1)
df
Out[250]:
   col1  col2  col3
0     1     4     2
1     2     3     3
2     3     2     1
3     4     1     4
In [251]:

df.ix[2,'col2':'col3']
Out[251]:
col2    2
col3    1
Name: 2, dtype: int64
+1

All Articles