Indexing a pandas dataframe by integer

Viewed 34194

I can't seem to find an elegant way to index a pandas.DataFrame by an integer index. In the following example I want to get the value 'a' from the first element of the 'A' column.

import pandas
df = pandas.DataFrame(
    {'A':['a','b', 'c'], 'B':['f', 'g', 'h']}, 
    index=[10,20,30]
    )

I would expect df['A'].ix[0] and df['A'][10] both to return 'a'. The df['A'][10] does return 'a', but df['A'].ix[0] throws a KeyError: 0. The only way I could think of to get the value 'a' based on the index 0 is to use the following approach.

df['A'][df['A'].index[0]]

Is there a shorter way to get 'a' out of the dataframe, using the 0 index?

Update

As of pandas 0.11 there is a another way to index by integer.

df.iloc[0] # integer based, gives the first row
df.loc[10] # label based, gives the row with label 10

This supersedes the irow approach .

2 Answers
Related