Unit 5-1
Unit 5-1
There are four collection data types in the Python programming language:
Tuples
Tuple is an immutable (unchangeable) collection of elements of different
data types. It is an ordered collection, so it preserves the order of
elements in which they were defined.
()
('Jeff', 'Bill', 'Steve', 'Yash')
(1, 2, 3, 4, 5)
(1, 'Steve', True, 25, 12000)
However, it is not necessary to enclose the tuple elements in
parentheses. The tuple object can include elements separated by a
comma without parentheses.
'Jeff'
<class 'string'>
(Jeff)
<class 'tuple'>
nums = (1, 2, 3, 4, 5)
print(nums[0]) # prints 1
print(nums[1]) # prints 2
print(nums[4]) # prints 5
Output:
Jeff
Bill
Steve
Yash
1
2
5
The tuple supports negative indexing also, the same as list type. The
negative index for the first element starts from -number of
elements and ends with -1 for the last element.
If the element at the specified index does not exist, then the error
"index out of range" will be thrown.
>>> names[5]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: tuple index out of range
Output:
Jeff Bill Steve Yash
However, you can delete an entire tuple using the del keyword.
Tuple Class
The underlying type of a tuple is the tuple class. Check the type of a variable using
the type() function.
nums = (1,2,3,4,5)
print('nums type: ', type(nums))
Output:
names type: <class 'tuple'>
nums type: <class 'tuple'>
Tuple Operations
Concatenation
Example –
>>> tuple1 = (1,3,5,7,9)
>>> tuple2 = (2,4,6,8,10)
>>> tuple1 + tuple2
(1, 3, 5, 7, 9, 2, 4, 6, 8, 10)
Repetition
Example –
>>> tuple1 = (‘Hello’,’World’)
>>> tuple1 * 3
(‘Hello’, ‘World’, ‘Hello’, ‘World’, ‘Hello’, ‘World’)
len()
Returns the length or the number of elements of the tuple passed as the argument.
Example –
>>> tuple1 = (10,20,30,40,50)
>>> len(tuple1)
5
tuple()
Example 1 –
>>> tuple1 = tuple()
>>> tuple1
( )
Example 2 –
>>> tuple1 = tuple(‘aeiou’)
>>> tuple1
(‘a’, ‘e’, ‘i’, ‘o’, ‘u’)
count()
Returns the number of times the given element appears in the tuple
Example –
>>> tuple1 = (10,20,30,10,40,10,50)
>>> tuple1.count(10)
3
>>> tuple1.count(90)
0
index()
Returns the index of the first occurrence of the element in the given tuple.
Example –
>>> tuple1 = (10,20,30,40,50)
>>> tuple1.index(30)
2
>>> tuple1.index(90)
ValueError: tuple.index(x): x not in tuple
sorted()
Takes elements in the tuple and returns a new sorted list. It should be noted that, sorted() does not make any
change to the original tuple.
Example –
>>> tuple1 = (“Rama”,”Heena”,”Raj”, “Mohsin”,”Aditya”)
>>> sorted(tuple1)
[‘Aditya’, ‘Heena’, ‘Mohsin’, ‘Raj’, ‘Rama’]
min()
Example –
>>> tuple1 = (19,12,56,18,9,87,34)
>>> min(tuple1)
9
max()
sum()
Example –
>>> tuple1 = (19,12,56,18,9,87,34)
>>> sum(tuple1)
235
Python Set
A Python set is the collection of the unordered items. Each element in the set must be unique, immutable, and
the sets remove the duplicate elements. Sets are mutable which means we can modify it after its creation.
Unlike other collections in Python, there is no index attached to the elements of the set, i.e., we cannot directly
access any element of the set by the index. However, we can print them all together, or we can get the list of
elements by looping through the set.
Creating a set
The set can be created by enclosing the comma-separated immutable items with the curly braces {}. Python also
provides the set() method, which can be used to create the set by the passed sequence.
Output:
print(Days)
print(type(Days))
print("looping through the set elements ... ")
for i in Days:
print(i)
Output:
It can contain any type of element such as integer, float, tuple etc. But mutable elements (list, dictionary, set) can't
be a member of set. Consider the following example.
Output:
<class 'set'>
In the above code, we have created two sets, the set set1 have immutable elements and set2 have one mutable
element as a list. While checking the type of set2, it raised an error, which means set can contain only immutable
elements.
Creating an empty set is a bit different because empty curly {} braces are also used to create a dictionary as well.
So Python provides the set() method used without an argument to create an empty set.
# Empty curly braces will create dictionary
set3 = {}
print(type(set3))
Output:
<class 'dict'>
<class 'set'>
Let's see what happened if we provide the duplicate element to the set.
set5 = {1,2,4,4,5,8,9,9,10}
print("Return set with unique elements:",set5)
Output:
In the above code, we can see that set5 consisted of multiple duplicate elements when we printed it remove the
duplicity from the set.
Output:
To add more than one item in the set, Python provides the update() method. It accepts iterable as an argument.
Output:
Output:
Python provides also the remove() method to remove the item from the set. Consider the following example to
remove the items using remove() method.
Output:
We can also use the pop() method to remove the item. Generally, the pop() method will always remove the last
item but the set is unordered, we can't determine which element will be popped from set.
Consider the following example to remove the item from the set using pop() method.
Output:
In the above code, the last element of the Month set is March but the pop() method removed the June and
January because the set is unordered and the pop() method could not determine the last element of the set.
Python provides the clear() method to remove all the items from the set.
Output:
If the key to be deleted from the set using discard() doesn't exist in the set, the Python will not give the error. The
program maintains its control flow.
On the other hand, if the item to be deleted from the set using remove() doesn't exist in the set, the Python will
raise an error.
Output:
The union of two sets is calculated by using the pipe (|) operator. The union of the two sets contains all the items
that are present in both the sets.
Consider the following example to calculate the union of two sets.
Output:
Python also provides the union() method which can also be used to calculate the union of two sets. Consider the
following example.
Days1 = {"Monday","Tuesday","Wednesday","Thursday"}
Days2 = {"Friday","Saturday","Sunday"}
print(Days1.union(Days2)) #printing the union of the sets
Output:
Signature
pop()
Parameters
No parameter.
Return
It returns deleted element or throws an error if the set is empty.
Output:
{1, 2, 3, 4, 5}
Element popped: 1
Remaining elements: {2, 3, 4, 5}
Element popped: 2
Remaining elements: {3, 4, 5}
Element popped: 3
Remaining elements: {4, 5}
Element popped: 4
Remaining elements: {5}
If the set is empty, it throws an error KeyError to the caller function. See the example.
Output:
{1, 2}
Element popped: 1
Remaining elements: {2}
Element popped: 2
Remaining elements: set()
Traceback (most recent call last):
File "main.py", line 13, in
el = set.pop()
KeyError: 'pop from an empty set'
The copy() method returns a shallow copy of the set in python. If we use “=” to copy a set to
another set, when we modify in the copied set, the changes are also reflected in the original set. So
we have to create a shallow copy of the set such that when we modify something in the copied set,
changes are not reflected back in the original set. Syntax:
set_name.copy()
Parameters:The copy() method for sets doesn’t take any parameters. Return value:The function
returns a shallow copy of the original set. Below is the implementation of the above function:
set1 = {1, 2, 3, 4}
set2 = set1.copy()
print(set2)
Output:
{1, 2, 3, 4}
# before adding
print 'before adding: '
print 'first: ',first
print 'second: ', second
# after adding
print 'after adding: '
print 'first: ', first
print 'second: ', second
Output:
before adding:
first: set(['s', 'e', 'k', 'g'])
second: set(['s', 'e', 'k', 'g'])
after adding:
first: set(['s', 'e', 'k', 'g'])
second: set(['s', 'e', 'k', 'g', 'f'])
test_set = {1, 2, 3, 4}
test_set.clear()
print("After clear() on test_set:", test_set)
Output:
After clear() on test_set: set()
# set of letters
GEEK = {"A", "B", "C"}
print('GEEK before clear:', GEEK)
# clearing vowels
GEEK.clear()
print('GEEK after clear:', GEEK)
Output:
GEEK before clear: {'B', 'C', 'A'}
GEEK after clear: set()
Python Dictionary
Python dictionary is an ordered collection (starting from Python 3.7) of items. It stores elements
in key/value pairs. Here, keys are unique identifiers that are associated with each value.
Let's see an example,
If we want to store information about countries and their capitals, we can create a dictionary with
country names as keys and capitals as values.
Keys Values
Nepal Kathmandu
Italy Rome
England London
print(capital_city)
Run Code
Output
Note: Here, keys and values both are of string type. We can also have keys and values of
different data types.
Example 1: Python Dictionary
# dictionary with keys and values of different data types
numbers = {1: "One", 2: "Two", 3: "Three"}
print(numbers)
Output
In the above example, we have created a dictionary named numbers. Here, keys are of integer
type and values are of string type.
Add Elements to a Python Dictionary
We can add elements to a dictionary using the name of the dictionary with []. For example,
capital_city = {"Nepal": "Kathmandu", "England": "London"}
print("Initial Dictionary: ",capital_city)
capital_city["Japan"] = "Tokyo"
Output
In the above example, we have created a dictionary named capital_city. Notice the line,
capital_city["Japan"] = "Tokyo"
Here, we have added a new element to capital_city with key: Japan and value: Tokyo.
Change Value of Dictionary
We can also use [] to change the value associated with a particular key. For example,
student_id[112] = "Stan"
In the above example, we have created a dictionary named student_id. Initially, the value
associated with the key 112 is "Kyle". Now, notice the line,
student_id[112] = "Stan"
Here, we have changed the value associated with the key 112 to "Stan".
Accessing Elements from Dictionary
In Python, we use the keys to access their corresponding values. For example,
student_id = {111: "Eric", 112: "Kyle", 113: "Butters"}
If we try to access the value of a key that doesn't exist, we'll get an error. For example,
student_id = {111: "Eric", 112: "Kyle", 113: "Butters"}
print(student_id[211])
del student_id[111]
Output
del student_id[111]
The del statement removes the element associated with the key 111.
We can also delete the whole dictionary using the del statement,
print(student_id)
print(numbers)
# Output: {}
clear() Syntax
The syntax of the clear() method is:
dictionary.clear()
Output
In the above example, we have used the clear() method to remove all the
items from the dictionary cityTemperature.
Here, the method removes every item from {"New York": 18, "Texas":
copied_marks = original_marks.copy()
dict.copy()
copy() Arguments
The copy() method doesn't take any arguments.
Output
Output
new: {}
original: {}
Here, when the new dictionary is cleared, the original dictionary is also
cleared.
The get() method returns the value for the specified key if the key is in the dictionary
# Output: 67
Syntax of Dictionary get()
The syntax of get() is:
dict.get(key[, value])
get() Parameters
get() method takes maximum of two parameters:
key - key to be searched in the dictionary
value (optional) - Value to be returned if the key is not found. The default value is None.
# value is provided
print('Salary: ', person.get('salary', 0.0))
Output
Name: Phill
Age: 22
Salary: None
Salary: 0.0
person = {}
Salary: None
Traceback (most recent call last):
File "", line 7, in
print(person['salary'])
KeyError: 'salary'
The pop() method removes and returns an element from a dictionary having the given key.
# create a dictionary
marks = { 'Physics': 67, 'Chemistry': 72, 'Math': 89 }
element = marks.pop('Chemistry')
dictionary.pop(key[, default])
pop() Parameters
pop() method takes two parameters:
key - key which is to be searched for removal
default - value which is to be returned when the key is not in the dictionary
element = sales.pop('apple')
Output
element = sales.pop('guava')
Output
KeyError: 'guava'
Output
dict.popitem()
Note: Before Python 3.7, the popitem() method returned and removed an arbitrary element (key,
value) pair from the dictionary.
Note: The popitem() method raises a KeyError error if the dictionary is empty.
Overview of numpy
If you are using Anaconda distribution, then you can use conda to
install NumPy. conda install numpy
import numpy
>>> numpy.__version__
'1.17.2'
NumPy is a library for the Python programming language, and it’s specifically
designed to help you work with data.
With NumPy, you can easily create arrays, which is a data structure that allows you
to store multiple values in a single variable.
Let us take a look at how to create NumPy arrays, copy and view arrays, reshape
arrays, and iterate over arrays.
One has to import the library in the program to use it. The module NumPy has an
array function in it which creates an array.
Creating an Array:
import numpy as np
print(arr)
Output:
[1 2 3 4 5]
We can also pass a tuple in the array function to create an array. 2
import numpy as np
print(arr)
Dimensions- Arrays:
0-D Arrays:
The following code will create a zero-dimensional array with a value 36.
import numpy as np
arr = np.array(36)
print(arr)
Output:
36
1-Dimensional Array:
The array that has Zero Dimensional arrays as its elements is a uni-dimensional or
1-D array.
import numpy as np
print(arr)
Output:
[1 2 3 4 5]
2-D Arrays are the ones that have 1-D arrays as its element. The following code will
create a 2-D array with 1,2,3 and 4,5,6 as its values. We can imagine 2d array as
combination of several 1d array.
import numpy as np
print(arr1)
Output:
[[1 2 3]
[4 5 6]]
Let us see an example of creating a 3-D array with two 2-D arrays:we can imagine
3d array to be combination of several 2d array.
import numpy as np
Output:
[[[1 2 3]
[4 5 6]]
[[1 2 3]
[4 5 6]]]
To identify the dimensions of the array, we can use ndim as shown below:
import numpy as np
a = np.array(36)
d = np.array([[[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]])
print(a.ndim)
print(d.ndim)
Output:
0
3
Introduction
While doing your data science or machine learning projects, you would
often be required to carry out some statistical operations. In this tutorial,
we will cover numpy statistical functions numpy mean, numpy mode,
numpy median and numpy standard deviation. All of
these statistical functions help in better understanding of data and also
facilitates in deciding what actions should be taken further on data.
In [1]:
import numpy as np
Commencing this tutorial with the mean function.
speed = [99,86,87,88,111,86,103,87,94,78,77,85,86]
What is the average, the middle, or the most common speed value?
Mean
To calculate the mean, find the sum of all values, and divide the sum by
the number of values:
(99+86+87+88+111+86+103+87+94+78+77+85+86) / 13 = 89.77
import numpy
speed = [99,86,87,88,111,86,103,87,94,78,77,85,86]
x = numpy.mean(speed)
print(x)
Median
The median value is the value in the middle, after you have sorted all the
values:
77, 78, 85, 86, 86, 86, 87, 87, 88, 94, 99, 103, 111
It is important that the numbers are sorted before you can find the
median.
import numpy
speed = [99,86,87,88,111,86,103,87,94,78,77,85,86]
x = numpy.median(speed)
print(x)
If there are two numbers in the middle, divide the sum of those numbers
by two.
77, 78, 85, 86, 86, 86, 87, 87, 94, 98, 99, 103
import numpy
speed = [99,86,87,88,86,103,87,94,78,77,85,86]
x = numpy.median(speed)
print(x)
Mode
The Mode value is the value that appears the most number of times:
99, 86, 87, 88, 111, 86, 103, 87, 94, 78, 77, 85, 86 = 86
The SciPy module has a method for this. Learn about the SciPy module in
our SciPy Tutorial.
Example
Use the SciPy mode() method to find the number that appears the most:
speed = [99,86,87,88,111,86,103,87,94,78,77,85,86]
x = stats.mode(speed)
print(x)
Standard deviation is a number that describes how spread out the values
are.
A low standard deviation means that most of the numbers are close to the
mean (average) value.
A high standard deviation means that the values are spread out over a
wider range.
speed = [86,87,88,86,87,85,86]
0.9
Meaning that most of the values are within the range of 0.9 from the
mean value, which is 86.4.
speed = [32,111,138,28,59,77,97]
37.85
Meaning that most of the values are within the range of 37.85 from the
mean value, which is 77.4.
As you can see, a higher standard deviation indicates that the values are
spread out over a wider range.
import numpy
speed = [86,87,88,86,87,85,86]
x = numpy.std(speed)
print(x)
import numpy
speed = [32,111,138,28,59,77,97]
x = numpy.std(speed)
print(x)
Variance
Variance is another number that indicates how spread out the values are.
In fact, if you take the square root of the variance, you get the standard
deviation!
Or the other way around, if you multiply the standard deviation by itself,
you get the variance!
(32+111+138+28+59+77+97) / 7 = 77.4
32 - 77.4 = -45.4
111 - 77.4 = 33.6
138 - 77.4 = 60.6
28 - 77.4 = -49.4
59 - 77.4 = -18.4
77 - 77.4 = - 0.4
97 - 77.4 = 19.6
(-45.4)2 = 2061.16
(33.6)2 = 1128.96
(60.6)2 = 3672.36
(-49.4)2 = 2440.36
(-18.4)2 = 338.56
(- 0.4)2 = 0.16
(19.6)2 = 384.16
(2061.16+1128.96+3672.36+2440.36+338.56+0.16+384.16) / 7 = 143
2.2
import numpy
speed = [32,111,138,28,59,77,97]
x = numpy.var(speed)
print(x)
Standard Deviation
√1432.25 = 37.85
Or, as in the example from before, use the NumPy to calculate the
standard deviation:
import numpy
speed = [32,111,138,28,59,77,97]
x = numpy.std(speed)
print(x)
Python Pandas
It is used for data analysis in Python and developed by Wes McKinney in 2008. Our Tutorial
provides all the basic and advanced concepts of Python Pandas, such as Numpy, Data
operation and Time Series
Data analysis requires lots of processing, such as restructuring, cleaning or merging, etc.
There are different tools are available for fast data processing, such as Numpy, Scipy,
Cython, and Panda. But we prefer Pandas because working with Pandas is fast, simple and
more expressive than other tools.
Pandas is built on top of the Numpy package, means Numpy is required for operating the
Pandas.
Before Pandas, Python was capable for data preparation, but it only provided limited support
for data analysis. So, Pandas came into the picture and enhanced the capabilities of data
analysis. It can perform five significant steps required for processing and analysis of data
irrespective of the origin of the data, i.e., load, manipulate, prepare, model, and analyze.
o It has a fast and efficient DataFrame object with the default and customized indexing.
o Used for reshaping and pivoting of the data sets.
o Group by data for aggregations and transformations.
o It is used for data alignment and integration of the missing data.
o Provide the functionality of Time Series.
o Process a variety of data sets in different formats like matrix data, tabular
heterogeneous, time series.
o Handle multiple operations of the data sets such as subsetting, slicing, filtering,
groupBy, re-ordering, and re-shaping.
o It integrates with the other libraries such as SciPy, and scikit-learn.
o Provides fast performance, and If you want to speed it, even more, you can use
the Cython.
Benefits of Pandas
o Data Representation: It represents the data in a form that is suited for data analysis
through its DataFrame and Series.
o Clear code: The clear API of the Pandas allows you to focus on the core part of the
code. So, it provides clear and concise code for the user.
The Pandas provides two data structures for processing the data, i.e., Series and DataFrame,
which are discussed below:
1) Series (Not in Syllabus)
It is defined as a one-dimensional array that is capable of storing various data types. The row
labels of series are called the index. We can easily convert the list, tuple, and dictionary into
series using "series' method. A Series cannot contain multiple columns. It has one parameter:
Before creating a Series, Firstly, we have to import the numpy module and then use array()
function in the program.
1. import pandas as pd
2. import numpy as np
3. info = np.array(['P','a','n','d','a','s'])
4. a = pd.Series(info)
5. print(a)
Output
0 P
1 a
2 n
3 d
4 a
5 s
dtype: object
Explanation: In this code, firstly, we have imported the pandas and numpy library with
the pd and np alias. Then, we have taken a variable named "info" that consist of an array of
some values. We have called the info variable through a Series method and defined it in an
"a" variable. The Series has printed by calling the print(a) method.
It is a widely used data structure of pandas and works with a two-dimensional array with
labeled axes (rows and columns). DataFrame is defined as a standard way to store data and
has two different indexes, i.e., row index and column index. It consists of the following
properties:
o The columns can be heterogeneous types like int, bool, and so on.
o It can be seen as a dictionary of Series structure where both the rows and columns are
indexed. It is denoted as "columns" in case of columns and "index" in case of rows.
Output
0
0 Python
1 Pandas
Explanation: In this code, we have defined a variable named "x" that consist of string
values. The DataFrame constructor is being called on a list to print the values.
Given a dictionary of equal length lists, task is to create a Pandas DataFrame from it.
There are various ways of creating a DataFrame in Pandas. One way is to convert a
dictionary containing lists of equal lengths as values. Let’s discuss how to create a Pandas
Dataframe from a dict of equal length lists with help of examples.
Example #1: Given a dictionary which contains format of cricket as keys and list of top five
teams as values.
rankings_pd
Output:
Example #2: Given three lists test_batsmen, odi_batsmen, t20_batsmen. So we first need to
convert this data into a dictionary and then convert the dictionary into DataFrame.
# Import pandas package
import pandas as pd
rankings_batsmen_pd
Output:
To access data from the CSV file, we require a function read_csv() that
retrieves data in the form of the Dataframe.
Syntax of read_csv()
Syntax:
pd.read_csv(filepath_or_buffer, sep=’ ,’ ,
header=’infer’, index_col=None, usecols=None, engine=None,
skiprows=None, nrows=None)
Parameters:
filepath_or_buffer: It is the location of the file which is to be retrieved
using this function. It accepts any string path or URL of the file.
sep: It stands for separator, default is ‘, ‘ as in CSV(comma separated
values).
header: It accepts int, a list of int, row numbers to use as the column
names, and the start of the data. If no names are passed, i.e.,
header=None, then, it will display the first column as 0, the second as 1,
and so on.
usecols: It is used to retrieve only selected columns from the CSV file.
nrows: It means a number of rows to be displayed from the dataset.
index_col: If None, there are no index numbers displayed along with
records.
skiprows: Skips passed rows in the new data frame.
Read CSV using Pandas read_csv
Before using this function, we must import the Pandas library, we will load
the CSV file.
# Import pandas
import pandas as pd
In this example, we will manipulate our existing CSV file and then add some
special characters to see how the sep parameter works.
df = pd.read_csv('example1.csv',
header=0,
usecols=["tip", "sex", "time"])
df
Example 3: Using index_col in read_csv()
Here, we use the “sex” index first and then the “tip” index, we can simply
reindex the header with index_col parameter.
Output:
df = pd.read_csv('example1.csv',
header=0,
usecols=["tip", "sex", "time"])
df
Output:
Example 3: Using index_col in read_csv()
Here, we use the “sex” index first and then the “tip” index, we can simply
reindex the header with index_col parameter.
df = pd.read_csv('example1.csv',
header=0,
index_col=["sex", "tip"],
usecols=["tip", "sex", "time"])
df
Output:
Example 4: Using nrows in read_csv()
Here, we just display only 5 rows using nrows parameter.
df = pd.read_csv('example1.csv',
header=0,
index_col=["tip", "sex"],
usecols=["tip", "sex", "time"],
nrows=5)
df
Output:
Example 5: Using skiprows in read_csv()
The skiprows help to skip some rows in CSV, i.e, here you will observe that
the upper row and the last row from the original CSV data have been
skipped.
pd.read_csv("example1.csv", skiprows =
[1,12])
Output:
Pandas DataFrame.loc[]
loc():
loc is label-based, which means that you have to specify rows and columns
based on their row and column labels.
Syntax:
loc[row_label, column_label]
# importing pandas as pd
import pandas as pd
Now we will use DataFrame.loc attribute to return the value present in the ‘Name’ column corresponding to the ‘Row_2’ label.
# return the value
result = df.loc['Row_2', 'Name']
As we can see in the output, the DataFrame.loc attribute has successfully returned the value present at the desired location in the
given DataFrame.
Example #2: Use DataFrame.loc attribute to return two of the column in the given Dataframe.
# importing pandas as pd
import pandas as pd
As we can see in the output, the DataFrame.loc attribute has successfully returned
the desired columns of the dataframe.
Python | Extracting rows using Pandas .iloc[]
Python is a great language for doing data analysis, primarily because of the
fantastic ecosystem of data-centric Python packages. Pandas is one of those
packages that makes importing and analyzing data much easier.
The Pandas library provides a unique method to retrieve rows from a Data
Frame. Dataframe.iloc[] method is used when the index label of a data frame is
something other than numeric series of 0, 1, 2, 3….n or in case the user doesn’t
know the index label. Rows can be extracted using an imaginary index position
which isn’t visible in the data frame.
iloc():
The Pandas library provides a unique method to retrieve rows from a Data
Frame. Dataframe.iloc[] method is used when the index label of a data
frame is something other than numeric series of 0, 1, 2, 3….n or in case the
user doesn’t know the index label. Rows can be extracted using an
imaginary index position which isn’t visible in the data frame.
Syntax:
iloc[row_position, column_position]
Example #1: Extracting single row and comparing with .loc[] In this example,
same index number row is extracted by both .iloc[] and.loc[] method and
compared. Since the index column by default is numeric, hence the index
label will also be integers.
Output:
As shown in the output image, the results returned by both methods are the
same.
Output:
As shown in the output image, the results returned by both methods are the
same.
Example #2: Extracting multiple rows with index In this example, multiple
rows are extracted, first by passing a list and then by passing integers to
extract rows between that range. After that, both the values are compared.
# comparing values
row1 == row2
Output:
As shown in the output image, the results returned by both methods are the
same. All values are True except values in the college column since those
were NaN values.
iloc is integer position-based, so you have to specify rows and columns by their
integer position values (0-based integer position).