Selecting items by integer location
In the following section, we will see how to select items by integer location.
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 code, which demonstrates different ways to select data using integer locations:
row_data = df.iloc[2] col_data = df.iloc[:,1] single_item = df.iloc[3,1] df1 = df.iloc[0:2, 1:]
This code results in the following data being stored in each of the corresponding variables:
row_data
:
item Sharpener inventory 12 unit_price 10.39 Name: 202034, dtype: object
col_data
:
100024 24 201024 32 202034 12...