Selecting items using mixed indexing
In the next section, we will see how to select items using mixed indexing.
Getting ready
Run the following code to load NumPy, and pandas, and to initialize a DataFrame
object:
import numpy as np import pandas as pd df1 = pd.DataFrame([['Pen', 24, 2.39], ['Eraser', 32, 1.29], ['Sharpener', 12, 10.39], ['Pencil', 42, 0.59 ]], index=[100024, 201024, 202034, 101122], columns = ['item', 'inventory', 'unit_price'])
How to do it...
Run the following commands, which provide examples of the ix
indexing method:
single_item = df.ix[100024, 1] df1 = df.ix[[201024,202034], :2]
The data returned by these calls is the following:
single_item
:
24
df1
:
item inventory 201024 Eraser 32 202034 Sharpener 12
How it works...
The ix
indexing method allows mixing integer locations with references by labels.
In the first example, df.ix[100024, 1]
, the row is...