Selecting a row of pandas series/dataframe by integer index
echoing @HYRY, see the new docs in 0.11
Here we have new operators,
.iloc to explicity support only integer indexing, and
.loc to explicity support only label indexing
e.g. imagine this scenario
In : df = pd.DataFrame(np.random.rand(5,2),index=range(0,10,2),columns=list('AB'))In : dfOut: A B0 1.068932 -0.7943072 -0.470056 1.1922114 -0.284561 0.7560296 1.037563 -0.2678208 -0.538478 -0.800654In : df.iloc[]Out: A B4 -0.284561 0.756029In : df.loc[]Out: A B2 -0.470056 1.192211
 slices the rows (by label location) only
The primary purpose of the DataFrame indexing operator,
 is to select columns.
When the indexing operator is passed a string or integer, it attempts to find a column with that particular name and return it as a Series.
So, in the question above:
df searches for a column name matching the integer value
2. This column does not exist and a
KeyError is raised.
The DataFrame indexing operator completely changes behavior to select rows when slice notation is used
Strangely, when given a slice, the DataFrame indexing operator selects rows and can do so by integer location or by index label.
This will slice beginning from the row with integer location 2 up to 3, exclusive of the last element. So, just a single row. The following selects rows beginning at integer location 6 up to but not including 20 by every third row.
You can also use slices consisting of string labels if your DataFrame index has strings in it. For more details, see this solution on .iloc vs .loc.
I almost never use this slice notation with the indexing operator as its not explicit and hardly ever used. When slicing by rows, stick with
You can think DataFrame as a dict of Series.
df[key] try to select the column index by
key and returns a Series object.
However slicing inside of  slices the rows, because it's a very common operation.
You can read the document for detail: