As we all know that python is widely used for data science. Python has excellent support for data science. Also, python provides a wide range of data science libraries for different kinds of operations. One of them is Pandas. The pandas library is the most favorite library for data scientists. The Pandas make data analysis way simpler than other libraries.
The pandas library provides many inbuilt functions to make operations on the data set. An iloc is one of its functions. The iloc in python is used to retrieve rows from the data set. This function is used when the indexing in the data set is not a number (0,1,2,3,4,…,n) or when the user does not know the exact name given to the index.
Table of Contents
Syntax of iloc in python :
pandas.dataframe.iloc[]
Parameters:
- Index position
- Return type
Example of iloc function in python
Example
import pandas
mydict = ({'Brand' : ['Lamborghini', 'Mustang', 'Porsche', 'BMW'],
'Year' : [2012, 2014, 2011, 2015],
'City' : ['Pune', 'Delhi', 'Mumbai', 'Delhi'],
'Mileage' : [5, 8, 12, 15]})
data = pandas.DataFrame(mydict)
Printing data of index 1 and 3
print(data.iloc[1])
print(data.iloc[3])
Output
Brand Mustang
Year 2014
City Delhi
Mileage 8
Name: 1, dtype: object
Brand BMW
Year 2015
City Delhi
Mileage 15
Name: 3, dtype: object
Printing data from range 1 to 4 using the slice operator
print(data.iloc[1:4])
Output
Brand Year City Mileage
1 Mustang 2014 Delhi 8
2 Porsche 2011 Mumbai 12
3 BMW 2015 Delhi 15
Printing records by passing boolean values to the iloc function.
print(data.iloc[[True,True,False,True]])
Output
Brand Year City Mileage
0 Lamborghini 2012 Pune 5
1 Mustang 2014 Delhi 8
3 BMW 2015 Delhi 15
Printing name and year of a car
print(data.iloc[0,[0]],"\n", data.iloc[0,[1]])
Output
Brand Lamborghini
Name: 0, dtype: object
Year 2012
Name: 0, dtype: object
What is the difference between loc and iloc in python pandas?
The loc and iloc in python are predefined in the pandas library. Both the functions are used to retrieve data from the data set. We will see the difference between both functions with an example.
Example
Importing pandas package
import pandas as pd
Creating a list of dictionaries
mydict = [{'Name': 'mars', 'number': 4, 'distance from sun':228000000, 'moon count': 2, 'life' : False},
{'Name': 'earth', 'number':3 , 'distance from sun':150000000, 'moon count': 1, 'life' : True},
{'Name': 'Jupiter', 'number': 5, 'distance from sun':757060000000, 'moon count': 53, 'life' : False},
{'Name': 'saturn', 'number': 6, 'distance from sun':14000000000, 'moon count': 82, 'life' : False},
{'Name': 'venus', 'number': 2, 'distance from sun': 1070000000, 'moon count': 0, 'life' : False},
{'Name': 'mercury', 'number': 1, 'distance from sun': 460000000, 'moon count': 0, 'life' : False}]
Converting it into a data frame
data = pd.DataFrame(mydict)
loc() :
The inbuilt loc() function is a label-based function. We can retrieve the data using labels given to the column and row. The loc() function accepts the boolean data unlike iloc() function. We can make many operations of the data frame using the loc function in python, like selecting specific rows, selecting rows within a range, adding constraints, etc.
1. Selecting column with an attribute name
Example :
print(data.__getattr__('Name'))
Output
0 mars
1 earth
2 jupiter
3 saturn
4 venus
5 mercury
2. Selecting rows index-wise.
Example
print(data.loc[[0,2,4]])
Output
Name number distance from sun moon count life
0 mars 4 228000000 2 False
2 Jupiter 5 757060000000 53 False
4 venus 2 1070000000 0 False
3. Selecting rows in range using the slice operator
Example
print(data.loc[1:4])
Output
Name number distance from sun moon count life
1 earth 3 150000000 1 True
2 Jupiter 5 757060000000 53 False
3 saturn 6 14000000000 82 False
4 venus 2 1070000000 0 False
iloc() :
The iloc in python is an index-based function. It is used when the programmer does not know the index name of the data set. We can not use the name of the index in this function. We have to pass the index number as a parameter to the function. The iloc function helps to retrieve the data from specific rows and columns. The iloc() function does not accept the boolean data like loc(). We will learn this function considering the above data set.
1. Selecting different row using indexing
Example
print(data.iloc[[1,2,4,5]])
Output
Name number distance from sun moon count life
1 earth 3 150000000 1 True
2 jupiter 5 757060000000 53 False
4 venus 2 1070000000 0 False
5 mercury 1 460000000 0 False
2. Selecting row within range using the slice operator
Example:
print(data.iloc[1:4])
Output
Name number distance from sun moon count life
1 earth 3 150000000 1 True
2 jupiter 5 757060000000 53 False
3 saturn 6 14000000000 82 False