24.25 Python Programming Language
Unit - 5 Python Collections and Library
Tuples
"Python tuples are a collection of elements of any data types just like lists, but tuples are
immutable i.e. we cannot change the elements of the tuple or the tuple itself, once it's
assigned where as we can change the elements of the list as they are mutable.
Creating a Tuples in Python
Tuples can only be created when they are being assigned, hence by placing all the elements
inside parenthesis, separated by a comma will create a tuple. Let's take a closer look at the
= Syntax: tuppleName = ('val1'/'val2",'val3’...)
tempTuple = ("apple’, "mango", ‘banana")
print (tempTuple) # ofp: (‘apple’, ‘mango', ‘banana')
"The parentheses are optional and hence a tuple can also be created without writing the
parenthesis. But it’s always a good practice to have the parenthesis, as it always increases
the code readability and understanding,
temp = ‘apple’, 'mango', ‘banana!
print (temp) # o/p: (‘apple', ‘mango', ‘banana')
Accessing Elements in a Python Tuple and Indexing:
& Accessing elements in a tuple is no different than accessing elements ina list.
+ As python follows 0 based indexing hence a tuple with n elements will have indices from 0
through n-1. An index in a tuple is accessed with the index operator [1
. Python also allows us to access elements of a collection via negative indices. When accessing
using a negative index, -1 represents the last element and -n represents the first index where
nis the length of the index.
t = (,2,3,4)
print (t[1]) print (t[-2])
2 3
Finding length of a tuple
= The len() method returns the length of the tuple i.e. no of elements in the tuple
t (11,12,15,16, "are", "hello")
print (len (t))
6
C.B. Patel Computer College24.25 Python Programming Language
Updating/Changing values Tuples in Python:
+ “Adding a new element or deleting one is not an option when dealing with tuples in python,
as they are immutable.
* Even the elements of the tuple cannot be updated until and unless the element is mutable
for example a list.
t = (1,2,3,4,5)
t{4] = 3
Traceback (most recent call last):
File "
", line 1, in
tla] = 3
TypeError: 'tuple' object does not support item assignment
* If the element is mutable example a list which is inside a tuple; then it can be changed or
removed or appended by using all methods of lists
t = (0,1,2,3,[5,6,71)
t= (0,1,2,3, (5, 6) 7]) t [4] .append (9)
print (t)
t[4] [0] = 8 tO, 1, 2; 9, (5, ©; T+ 31h
i t [4] . remove (5)
print (t) print (t)
(0, 1, 2, 3, [8, 6 7])=\¢o, 1, 2, 3, [6, 7, 91)
" _Tuples in python can definitely be reassigned, which is very different from updating a tuple.
Reassigning a tuple is redefining entire the tuple elements again.
t=("Hello", "World", "One"
print (t) # Now t has string elements. Tuple redefined again.
('Hello', ‘World', 'One')
"Just like strings, We can also concate two or more tuples to form a new tuple
using ‘#’ operation.
+ The result here is a python tuple and not a string,
tL = (1,2,3,4)
t2 = (5,6,7/8)
£3 = t1 + t2
print (t3)
(1p 2, 3y 4e 5, 6 7 8)
* Repetition of Python tuples can be done using the *’ operator
tl = (1,)*5
print (t1)
a 1,14 »
35|Page C.B. Patel Computer College24-25 Python Programming Language
Deleting Tuples in Python:
‘+ python tuples being immutable cannot be updated or deleted.
"We can delete a tuple as a whole, but deleting a specific value/element in a tuple is not
possible.
t= (1,2,3,4)
t.remove (1)
Traceback (most recent call last):
File "", line 1, in
‘t. remove (1)
AttributeError: ‘tuple’ object has no attribute 'remove'
del t
print (t)
Traceback (most recent call last):
File “", line 1, in
print (t)
NameError: name 't' is not defined
+ There's no method remove to remove an element from tuple but we can delete entire tuple
using the del keyword. \
‘Once aeleted, the tuple cannot be accessed again and trying to print wil ive an error.
1
\
Slicing in tuples: * a \ |
Slicing in tuples works the same as it works for a String slicing or any ater sequence of
elements.
Slice isan gperator that allows fetching a sub collection (inthis case a sub tuple) from @
collection by slicing it from a start index and a stop index. /
t= (1, 2/3, 4,5, 6, 7,8, 9)
print (t(1:])
(2, 3, 4, 5, 6, Ty 8, 9) -
print (t[2:6]) print (t[-5:-1])
\(3, 4, 5, 6) (5, 6, 7, 8)
Creating a tuple using tuple() (Converting a List to a tuple)
We can also create a tuple using tuple().
= To typecast to tuple, we can simply use tuple(list_name).
36 | Page C.B. Patel Computer College24-25 Python Programming Language
index() method of Tuple
The index() method returns the index of the specified element in the tuple.
element - the item to scan
start_index (optional) - start scanning the element from the start_index
end_index (optional) - stop scanning the element at the end_index (no including the end
index)
v= (tat, tet, ti", tot, 'u")
print (v.index('e'))
1
print (v.index("e',1,2))
L
print (v.index('u',0,4)) ~
Traceback (most recent call last): \,
File "", line 1, in
print (v.index('"u',0,4))
tuple.index(x): x not in tuple
«a \
sments in the tuple.
method of Tuple —
() method returns the number of times the specified element appears
ists are mutable. Tuples are immutable.
Iterations are time-consuming. Iterations are comparatively faster.
To perform operations like insert, __| Tuples are better to access elements
delete etc., lists are better.
Lists have many built-in methods _| Tuples have fewer built-in methods
Lists consume more memory. Tuples consume less memory than lists.
37 | Page C.B. Patel Computer College24-25 Python Programming Language
Sets in Python
+ A Set is an unordered collection data type that is iterable, mutable, and has no duplicate
elements.
= Sets are represented by { } (values enclosed in curly braces).
+ Since sets are unordered, we cannot access items using indexes as we do in lists.
Characteristics of a set
Y Unordered: Set doesn’t maintain the order of the data insertion.
¥ Unchangeable: Set are immutable and we can’t modify items.
¥ Heterogeneous: Set can contains data of all types
Y Unique: Set doesn’t allows duplicates items
*
Creating a Set /
‘There are following two ways to create a set \
items inside the curly brackets (). The
+ Using curly brackets: by just enclo:
individual values arecorhma-separated. >
20, ‘essa’, 35.75}
+ Using set() constructor: The set object is of type class ‘set’. So we can create a set by calling
the constructor of class ‘set’. The items we pass while calling are of the type iterable, We can
«pass items to the set constructor in
Create a set from a list . .
«if we create a set with duplicate items it will store an item only once and delete all duplicate
items. -
= Creating a set from an iterable like a list will remove duplicate items from a list.
Empty set
38 [Page C.B. Patel Computer College24-25 Python Programming Language
‘+ When we don’t pass any item to the set() constructor then it will create an empty set.
Note: empty_set = {} will not create empty set but will create empty dictionary
Accessing items ofa set
The items of the set are unordered and they don’t have any index number.
+ In order to access the items of a set, we need to iterate through the set object using
a ‘for’ loop.
Methods of. / ox, \
1. add{) — adds a given element to a set only ifthe element is not present in the set in Python.
|
We need to pass
ber of arguments.
3. copy() ~ The copy() method is used to create a shallow copy of the Set. If we use “=” to copy
1 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.
39| Page C.B. Patel Computer CollegePython Programming Language
Methods to Remove elements
remove() | To remove a single item from a set. This method will take one parameter, which is
the item to be removed from the set. Throws a keyerror if an item not present in
the original set
discard() | To remove a single item that may or may not be present in the set. This method
also takes one parameter, which js the item to be removed. If that item is present,
pop()
clear()
del set | Delete the entire set
4. union() - It is a function that returns a set with all elements of the original set and also the
specified sets. Since it returns a set so all the items will have only one appearance. If two
sets contain the same value, then the item will appear only once. The union of two sets can
also be calculated by using the or (|) operator.
40 | Page C.B. Patel Computer CollegePython Programming Language
5. _ intersection() — returnsa new set with an element that is common to all set. The
intersection of two given sets is the largest set, which contains all the elements that
are common to both sets. The intersection of two given sets A and B is a set which consists
of all the elements which are common to both A and B. The intersection of two sets can also
be calculated by using the and (&)
6.
not in B and (set B - set A) will be the elements present
difference of two sets can also be calculated by using the (~) operator.
ai[Page C.B. Patel Computer College24-25 Python Programming Language
Dictionary in Python
+ In python a dictionary is the collection of key-value pairs where the value can be any python
object whereas the keys are unique identifiers that are associated with each value., i.e.,
Numbers, string or tuple.
Creating a dictionary
«The dictionary can be created by usit ue pairs which are separated by
‘comma(,) and enclosed
a2|Page C.B. Patel Computer College24-25 Python Programming Language
Adding the dictionary values
+ Addition of elements can be done in multiple ways.
"We can add elements to a dictionary using the name of the dictionary with (J. One value at a
time can be added to a Dictionary by defining value along with the key e.g. Dict{Key] =
‘value’.
© Updating an existing value in a Dictionary can be done by using the bui
Nested key values can also be added to an existing
in update() method.
1
Accessing the dictionary values
A / A
* The data can be accessed in the list and tuple by using the indexing.
However, the values can be accessed in the dictionary by using the keys as keys are unique in
‘the dictionary. A obs
+ There is also a method called gett) that will also help in accessing the element from a
method accepts key as argument and returns the valu
43 | Page C.B. Patel Computer College24-25 Python Programming Language
Updating dictionary values
"The dictionary is a mutable data type, and its values can be updated by using the specific
keys.
Student= ("Name": "Kiran", "Age": 22, "Rollno
Student ("Name”] = “Akash”
print (Student)
#o/p: {'Name': ‘Akash’, 'Age': 22, 'Rollno': 562}
Student ["Per”] = 59.65
#2er Key added not updated because it was not present
¥o/p: {'Name': ‘Akash’, ‘Age': 22, *Rollno': 562,"Per!:59.65}
Methods of Dictionary
1. clear() - The clear() method removes all items from the dictionary.
dict.clear ()
Student= ("Name": "Kiran", "Age": 22, "Rollno":562}
student clear ()
Print (*Student=", Student) #o/p: Student =}
Sl= {"Name": "Kiran", "Age": 22, "Rollno":562
s2 sl
1.clear() #clears both sl and s2 as referenc
print (“Student: ,S1,"Student2=",S2)
#o/p: Studenti={} Student2={}
{"Name"; "Kiran", "Age": 22, "Rollno
s2 = s1
S1 = {} #clears only S1
print ("Student1=",81,”Student2=", 52)
¥o/p: Student1=(}
Student2=("Name": "Kiran", "Age": 22, "Rollno'
2. copy() - copy() method returns a shallow copy of the dictionary. This method doesn’t
modify the original, dictionary just returns copy of the dictionary.
new = original_dict.copy()
Student= {"Name": "Kiran", "Age": 22, "Rollno"
Student2 = Student.copy()
4a | Page C.B. Patel Computer College24-25 Python Programming Language
‘Syntax: dict.pop(key, def)
Key: The key whose key-value pair has to be returned
di return if specified key is not present,
It returns Value associated to deleted key-value pair, if Key is present. Default value if
specified if key is not present. KeyError, if key not present and default value not specified.
om the dictionary and
45|Page C.B. Patel Computer College24.25 Python Programming Language
Introduction to Numpy and Pandas
NumPy stands for ‘Numerical Python’. It
‘computing with Python,
‘With NumPy, we can easily create arrays, which is a data structure that allows to store
multiple values in a single variable.
‘a package for data analysis and scientific
In particular, NumPy arrays provide an efficient way of storing and manipulating data.
Numpy also includes a number of functions that make it easy to perform mathematical
operations on arrays.
It provides a multidimensional array object, various derived objects (such as masked arrays
and matrices), and routines for fast operations on arrays, including mathematical, logical,
sorting, selecting, /O, basic linear algebra, basic statistical operations, etc.
In general, NumPy is a general-purpose array-processing package.
‘At the core of the NumPy package, is the ndarray object. It has n-dimensional arrays of
homogeneous data types, with many operations being performed in compiled code for
performance.
The powerful n-dimensional array in NumPy speeds-up data processing.
NumPy can be easily interfaced with other Python packages and provides tools for
integrating with other programming languages like C, C++ etc.
Installing NumPy
NumPy can be installed by typing following command:
C:\Users\...>pip insti
numpy
Using Numpy in Python
‘We can use NumPy in Python by importing it in the python program.
We can import NumPy in Python using the import statement.
The code above imports the numpy library in our program as an alias np.
After this import statement, we can use NumPy functions and objects by calling them
with np.
Numpy Array
Array in Numpy is a table of elements (usually numbers), all of the same type, indexed by a
tuple of positive integers.
In Numpy, number of dimensions of the array is called rank of the array.
A tuple of integers giving the size of the array along each dimension is known as shape of the
array.
46 | Page C.B. Patel Computer College24.25 Python Programming Language
‘An array class in Numpy is called as ndarray. Elements in Numpy arrays are accessed by
Using square brackets and can be initialized by using nested Python Lists.
NumPy arrays are used to store lists of numerical data, vectors and matrices
The NumPy library has a large set of built-in functions for creating, manipulating, and
transforming NumPy arrays.
Why Numpy Arrays are fast?
Numpy arrays are written. mostly in C language, Being written in C, the NumPy arrays are
stored in contiguous memory locations which makes them accessible and easier to modify.
Homogeneous Data: NumPy arrays store elements of the same data type, making them
more compact and memory-efficient than lists.
Fixed Data Type: NumPy arrays have a fixed data type, reducing memory overhead by
eliminating the need to store type information for each element.
Contiguous Memory: NumPy arrays store elements in sequential memory locations,
reducing fragmenta
n and allowing for efficient access.
Difference Between List and Array (ndarray)
List Array (ndarray)
List can have elements of different data types
for example, [1,3.4, ‘hello’, ‘a@’]
‘All elements of an array are of same data
type for example, an array of floats may be:
[1.2, 5.4, 2.7]
Elements of a list are not stored contiguously
in memory.
‘Array elements are stored in contiguous
memory locations. This makes operations on
arrays faster than lists.
Lists do not support element wise operations,
for example, addition, multiplication, etc.
because elements may not be of same type.
lists take more space in memory and are less
efficient.
‘Arrays support element wise operations. For
example, if A1 is an array, itis possible to say
‘A1/3 to divide each element of the array by 3.
NumPy array takes up less space in memory
as compared toa list because arrays do not
require storing datatype of each element
separately.
List is a part of core Python.
Array (ndarray) is a part of NumPy library.
Patel
age c.B
Computer College24.25 Python Programming Language
Properties of np Array
= ndarray.ndim will tell you the number of axes, or dimensions, of the array.
= ndarray.size will tell you the total number of elements of the array. This is the product of the
‘elements of the array’s shape.
‘= ndarray.shape will display a tuple of integers that indicate the number of elements stored
along each dimension of the array. If, for example, you have a 2-D array with 2 rows and 3
columns, the shape of your array is (2, 3).
Creating a numpy Array
= To create a NumPy array, you can use the function np.array()
# Python program for
4 Creation of Arrays
import numpy as/np
# creating the list
list = [100, 200, 300, 400]
# creating/1-d array from list
n= np.array(list)
print(n)
# Creating @ rank 2 Array
arr = np.array([1, 2, 3])
print("Array\with Rank 1: \n",arr)
# Creating @ rank 2 Array (2D Array)
arr = np.array([[1, 2, 3],
[4 5, 6]])
print("Array with Rank 2: \n", arr)
# Creating an’array from tuple
arr = np.array((1, 3, 2))
print("\nArray created using "
“passed tuple:\n", arr)
= 2-D Arrays are the ones that have 1-D arrays as its element. The following code will create a
2-D array.
a2 =np.array(([1, 2, 3, 4], [5, 6, 7, 8), [9, 10, 11, 12)])
print(a2)
print(a2.ndim)
np.linspace()
= Linspace() creates evenly space numerical elements between two given limits.
48 |
ge C.B. Patel Computer College24.25 Python Programming Language
# inporting the module
import numpy as np
# creating 1-d array
x = np.linspace(3, 10, 3)
print (x) # ofp [ 3. 6.5 10. ]
np.zeroes()
= The numpy.zeros() function returns a new array of given shape and type, with zeros.
import numpy as np
arr = np.zeros(5)
print(arr) Ho/p: [®. @. @. @. 0]
np.ones()
= The numpy.ones() function returns a new array of given shape and type, with ones.
>>>|import’ numpy as np
>>>a = np.ones (2)
>>> print (a)
(1. 1.)
np.arange()
= Itreturns evenly spaced values within a given interval. Step size is specified.
‘Syntax: numpy.arange(start, stop, step, dtype)
‘= start is the start of an interval. if omitted, default to 0.
* Stop is the end of interval (not including stop number).
step is spacing between values, default is 1
= Dtypeis data type of resulting ndarray. If not given, data type of input is used.
import numpy as np
al=np.arange (3,10,3)
print (al) O/p: [36 9]
np.eye()
= It create identity Matrix using shape provided as parameters
import numpy as np ((1. 0. 0.]
al=np.eye (3) [0. Le 0.1
print (al) ofp: (0. @. 1.1)
np.reshape()
The numpy.reshape() function shapes an array without changing the data of the array.
numpy.reshape(array, shape, order ='C’)
= array: Input array
+ shape: [int or tuples of int] e.g. if we are arranging an array with 10 elements then shaping it
like numpy.reshape(4, 8) is wrong; we can do numpy.reshapel2, 5) or (5, 2).
491
ge C.B. Patel Computer College24.25 Python Programming Language
* order: [C-contiguous, F-contiguous, A-contiguous; optional]
a2 = np.array([1,2,3,4,5,6])
a3 = a2.reshape(2,3)
print(a2) op: [1,2,3,4,5,6]
print(a3) op: {(1,2,3),(4,5,6]] #Shaped into two rows 3 cols
Accessing the array Index:
* Ina numpy array, indexing or accessing the array index can be done in multiple ways.
= To print a range of an array, slicing is done.
= Slicing of an array is defining a range in a new array which is used to print a range of
elements from the original array.
© We pass slice instead of index like this: [start:end].
© We can alsodefine the step, like this: [start:end:step].
© If we don't pass start its considered 0
°
If we don't pass end its considered length of array in that dimension
If we don't pass step its considered 1
© The result includes the start index, but excludes the end index.
+ Since, sliced array holds a range of elements of the original array, modifying content with the
help of sliced array modifies the original array content.
= We can index and slice NumPy arrays in the same ways we slice Python ists,
>>> data = np.array([1, 25 3])
>>> data[1]
2
>>> data[@:2]
array({1, 2])
>>> data[1:]
array([2, 3])
>>> data[-2:]
array({2, 3])
= We can easily print all of the values ithe array that are less than 5.
>>> a = np.array([T1., 2, 3, 4], [5, 6, 7, 8], 19, 10, 11, 12]])
>>> print(ala < 51)
1234)
Numpy - Statistical Functions
= NumPy has quite a few useful statistical functions for finding minimum, maximum,
percentile standard deviation and variance, etc. from the given elements in the array,
C.B. Patel Computer College24.25 Python Programming Language
Numpy methods:
Mean:
* Arithmetic mean is the sum of elements along an axis divided by the number of elements in
the array. The general form of this function is:
syntax:
var=numpy.mean(a, axis-None, dtype=None, out=None, keepdims=
Example:
import numpy as np
a= np.array((5,6,7])
print(’Array i
print(’Mean :
no value>)
# O/p: Array is : [567] Mean :6.0
Median:
* Median is the middle element of the array. The formula differs for odd and even sets as
follow:
For Odd no. of elements => (n+1)/2
For Even no, of elements => n/2 and n/2 +1
= Itcan calculate the median for both one-dimensional and multi-dimensional arrays.
= Median separates the higher and lower range of data values.
Syntax: var = numpy.median()
= It computes the median along the specified axis.
import numpy as np
a= np.array({5,6,7))
print(‘Acray is a)
print(’Median ; 'np.median(a)) O/p: Array is: [567] Median ; 6.0
a= np.array((5,6,7,8])
print(‘Array is :',a)
print('Median ; ';np.median(a)) O/p: Array is :[567,8] Median ; 6.5
std
= std() represents Standard deviation is the square root of the average of square deviations
from mean. The formula for standard deviation
aN
ye?
a
The function name feptionaly
Spopuation =
, axis=|, dtype=|, |adof=|)
t t t
Sfrequired) eancard des Cees aletion”
Teptene (options
51|
C.B. Patel Computer College24.25 Python Programming Language
import numpy as np
a= np.array((5,6,7])
print(‘Array is :',a)
print('Standard deviation : 'np.std(a))
O/p: Array is: [5 6 7]
Standard deviation: 0.816496580927726
Variance
= Compute the variance along the specified axis.
Returns the variance of the array elements, a measure of the spread of a distribution.
x)
ota Ele -¥)
n
Syntax:
‘The data type
of the output
The function name (eptional)
+
np-var( a=,
+
, dtype= , ddof= )
+
The oto operate on commute tho freedom used in
(required) Standard dev the calculation
(ptional) (ptional)
import numpy as np
arr = (20, 2, 7,1, 34]
print("arr :", arr)
print("var of arr :", np.var(are))
ofp: arr : (20, 2, 7, 1, 34] var of arr: 158.16
Mode
* Mode refers to the most repeating element in the array.
‘= We can find the mode from the NumPy array by using the following methods.
1. Calculate the Mode of a NumPy Array Using the scipy.stats.mode Function
‘= The scipy.stats module is a part of the SciPy library, which builds on NumPy to provide
additional functionality for statistical analysis.
scipy.stats.mode(a, axis=0, nan_policy="propagate")
a: Thisis the input array or object that can be converted to an array.
= axis (Optional): The axis along which the mode is calculated. By default, itis 0
= nan_policy (Optional): This defines how to handle when input contains NaN.
from scipy.stats import mode
import numpy as np
data = nparray({1, 2, 2, 3, 4,5,5,5, 6])
52|
C.B. Patel Computer College24.25 Python Programming Language
‘mode_result = mode(data)
print("Mode:", mode_result.mode) # Mode :5
print("Count:", mode result.count). # Count : 3
2. Calculate the Mode of a NumPy Array Using the statistics Module
= The statistics module is a part of the Python standard library, offering various functions for
statistical calculations.
‘= Among these functions is mode, which calculates the mode of a dataset.
import statisties
import numpy as np
data = np.array({1, 2, 2, 3, 4,5, 5, 5, 6])
mode_result = statistics. mode(data)
print("Mode:", mode_result) Ho/p: Mode : 3
Mathematical Operations on Numpy Arrays
* Itis possible to perform various mathematical oper
etc. on the elements of an array.
= Also, the functions of ‘math’ module can be applied to the elements of the array.
ions like addition, subtraction, division,
Scalar Addition
* Scalars can be added and subtracted from arrays and arrays can be added and subtracted
from each other.
* To add the a value to every element of an array, we can write: a =a + value
import numpy as np
arr = np.array((10, 20, 30, 40))
arr=arrtS
print(arr) ofp: [15,25,35,45]
* To add two arrays, a3 =al + a2 (Two arrays to be added must have same shape)
import numpy as np
al =np.array([10, 20, 30, 40])
a2 =np.array([1, 2, 3, 4])
a3 = al+a2
ad=al-a2
print(a3) ofp: (11,21,31,41]
print(aa) o/p: [9,18,27,36]
= Inthe same way, we can multiply and divide two arrays with each other.
+ Some other operations on Numpy Array includes
53]
ge C.B. Patel Computer College24-25 Python Programming Language
Function Meaning
labstarr) (calculates absolute value of each element in the array ‘arr’
Ksartiarr) (calculates square root value of each element in the array ‘arr’.
Jpower(arr,n) __ [Returns power of each element in the array ‘arr’ when raised to the power of 'n’
lsum(arr) IReturns sum of all the elements in the array ‘arr’,
lprod(arr) IReturns product of all the elements in the array ‘arr.
Imin(arr) [Returns smallest element in the array ‘arr.
|max(arr) IReturns biggest element in the array ‘arr’
largmin(arr) |Gives index of the smallest element in the array. Counting starts from 0.
largmax(arr) (Gives index of the biggest element in the array. Counting starts from 0.
lunique(arr) (Gives an array that contains unique elements of the array ‘art’
lsort(arr) (Gives an array with sorted elements of the array ‘art’ in ascending order.
|concatenate([a,b])|Returns an array after joining a, b arrays.
import numpy as np
al =np.array([25,36,-49,64,81,-100})
a2 = np.arrayi(1,2,3,4,5,6])
print(*Array
print(*Array 2
print("Abs = ",np.abs(a1))
print("Sqrt =",np.sqrt(abs(a1)))
print("Power = ",np.power(a1,2))
print("Sum =",np.sum(a1))
print("Prod =",np.prod(at))
print("Min = ",np.min(at))
print("Max =",np.max(a1))
print("ArgMin= ",np.argmin(at))
print("ArgMax= ",np.argmax(a1))
print("Unique=",np.unique(at))
print("Asc Sort= ",np.sort(a1))
print("Asc Sort= ",np.concatenate({a1,a2]))
Arrayl= [ 25 36 -49 64 61 -100]
12345 6)
[25 36 49 64 81 100)
(5. 6. 7. 8. 9. 10.)
[_ 625 "1296 2401 4096 6561 10000)
57
1386603520
[-100 -49 25 36 64 81)
[-100 -49 25 36 64 81)
(25 36 -49 64 e1-100 1 2
C.B. Patel Computer College
624.25 Python Programming Language
Pandas Dataframe
Pandas is a very popular library for working with data.
= It isa package useful for data analysis and manipulation.
Pandas provide an easy way to create, manipulate and wrangle the data.
Pandas provide powerful and easy-to-use data structures, as well as the means to quickly
perform operations on these structures.
It is built on packages like NumPy and Matplotlib and gives us a single, convenient place to
do most of our data analysis and visualisation work.
Data scientists use Pandas for its following advantages:
= Easily handles missing data.
* It uses Series for one-dimensional data structure and Dataframe for multi-dimensional data
structure.
= It provides an efficient way to slice the data.
= It provides a flexible way to merge, concatenate or reshape the data,
Installing Pandas
“pip” command is used to install Pandas.
‘For this, open the location of pip storage in command prompt (cmd).
= Type - pip install pandas
DATA STRUCTURE IN PANDAS
= Adata structure is a way to arrange the data in such a way that so it can be accessed quickly
and we can perform various operation on this data like- retrieval, deletion, modification etc.
* Pandas deals with 3 data structure-
©. Series
© Data Frame
©. Panel
Series
The Series is the core object of the pandas library.
A pandas Series is very similar to a one-dimensional NumPy array, but it has additional
functionality that allows values in the Series to be indexed using labels. This labeling is useful
when you are storing pieces of data that have other data associated with them.
* Aseries has two parts-
© Data part (An array of actual data)
© Associated index with data (associated array of indexes or data labels)
Points to remember:
= We can say that Series is a labeled one-dimensional array which can hold any type of data.
* Data of Series is always mutable, means it can be changed.
"= But the size of Data of Series is always immutable, means it cannot be changed.
age C.B. Patel Computer College24-25 Python Programming Language
= Series may be considered as a Data Structure with two arrays out which one array works as
Index (Labels) and the second array works as original Data.
= Row Labels in Series are called Index.
Creation of Series from Scalar Values
>>> import pandas as pd #import Pandas with alias pd
>>> series1 = pd.Series([10,20,30]) #create a Series
>>> print(series1) #Display the series
Creating a series from nparray
* A Series holds items of anyone data type and can be created by sending in a scalar value,
Python list, dictionary, or ndarray as a parameter to the pandas Series() constructor.
= Ifa dictionary is sent in, the keys may be Used as the indices.
ages = np.array([13,25,19])
series = pd.Series(ages) 5
print(series1) 2
e a3
25
19
!
1
1
dtype: intea
= When printing a Series, the data type of its elements is also printed.
= To customize the indices of a Series object, use the index argument of the Series constructor.
ages = np.array({13,25,19}) me 2
seriest = pd.Series(ages,index=[‘Emma’, Swetha’,'Seraih']) os, | ao
print(seriest) dtype: intes
Creating a series from a dictionary
= Python dictionary has key: value pairs and a value can be quickly retrieved when its key is
known
* Dictionary keys can be used to construct an index for a Series, as shown in the following
example.
= Here, keys of the dictionary dict become indices in the series.
>>>|import pandas as pd
>>>|dict1 = {"RollNo":1, "Name": "Abc", "Age":22,"Per":85.75)
>>>|ser = pd.Series (dict1)
>>> print (ser)
RollNo 1
Name Abe
Age a
Per 85.75
dtype: object
ge C.B. Patel Computer College24.25 Python Programming Language
Slicing and Indexing
* Indexing and Slicing in Series is similar to that for NumPy arrays, and is used to access or
extract elements in a series.
Indexes are of two types: positional index (0,1,2) and labelled index (name).
= We can define which part of the series is to be sliced by specifying the start and end
parameters [start :end] with the series name.
print (ser["Age"])
22 # indexing on label name
print (ser[1])
Abc index on positional index
print (ser[1:2])
Name Abe
dtype: object
print (ser[0:2])
Rol1No i
Name Abe
dtype: object
#slicing starting from index 1 to 2
= We can perform mathematical operations on Series like Numpy arrays on each element of
the series.
= While performing mathematical operations on series, index matching is implemented and all
missing values are filled in with NaN by default.
Methods of Series
Series.head{(n)
= head(n) Returns the first n members of the series.
‘= If the value for n is not passed, then by default n takes'5 and the first five members are
displayed.
Series.count()
= Returns the number of non-NaN values in the Series
Series.tail(n)
= Returns the last n members of the series.
= If the value for n is not passed, then by default n takes 5 and the last five members are
displayed
C.B. Patel Computer College24.25 Python Programming Language
Tmport pandas as pd print (ser.head(5))
import numpy as np 01
ser = pd.Series(np.arange(1,10,1))
iL 2
cr 2 3
- 2 3 4
Bog 45
= 4 dtype: int32
a. print (ser.tail (3)
aS 6 7
6 7 RG
7 8 : .
8 9 8 9
di : int32 dtype: int32
print (ser.count (})
9
Pandas DataFrames
= DataFrames are at the center of pandas.
= ADataFrame is a two-dimensional labelled data structure like a table of MySQL.
= It contains rows and columns, and therefore has both a row and column index.
= Each column can have a different type of value such as numeric, string, boolean, etc., as in
tables of a database,
a = =]
|
INDEX bara
Properties of DataFrame
C.B. Patel Computer College24-25 Python Programming Language
= Adata frame can be created using any of the following-
1. Series
2. Lists
3. Dictionary
4. Anumpy 2D array
= We will use pd.DataFrame() function to create the dataframe
Note:
+ To create DataFrame from dict of narray/list, all the narray/list must be of same length.
'= Ifindex is passed then the length of index should be equal to the length of arrays.
Creation of an empty DataFrame
‘An empty DataFrame can be created as follows:
import pandas as pd O/p: Empty DataFrame
df = pd.DataFrame() Columns: {]
print(at) Index: ()
Creation of DataFrame from NumPy ndarrays
= Consider the following three NumPy ndarrays.
= Let us create a simple DataFrame without any column labels, using a single ndarray:
import numpy as np O/p: 0
import pandas as pd 010
arr = np.array([10,20,30,50)) 120
df= pd.DataFrame(arr) 230
print(df) 350
= We can create a DataFrame using more than one ndarrays.
import numpy as np
import pandas as pd
arri = np.array({10,20,30,50))
arr = np.array({11,12,13,14]))
arr = np.array({100,200,300,500})
59|Page C.B. Patel Computer College24-25 Python Programming Language
df = pd.DataFrame([arri,arr2,arr3), columns=[Colt', Col2",'Col3",Cola')})
print(df)
O/p:
Cell €ol2 Gsl3 Cold
0 10 20 30 50
1 il 12 13 14
2 100 200 300 500
Creating a dataframe from a Series
import pandas as pd o
s= pd.Series([‘a‘,’b','c’,'d']) =
df=pd.DataFrame(s) 2
print(df)
a
Creating a dataframe from a Dictionary of Equal length Lists
= Create a DataFrame from multiple lists by passing a dict whose value are lists.
The keys of the dictionary are used as column labels.
= The lists can also be ndarrays.
‘The lists/ndarrays must all be the same length.
import pandas as pd
stud = ( "Name": ("Akash", "Bhavin", "Chirag", "Dilip"],
‘Age': [21,20, 21,20],
"Per! :[85.6,76.8,89.76, 56.45]
)
df = pd.DataFrame (stud)
print ("Dictionary =", stud)
print ("")
print (df)
(ene ", *Bhavin', "Chirag', ‘Dilip'], "Age's [2L, 20, 21, 201, "Per": 185.6, 16.8, 89.76,
E
Creating a dataframe from a List of Dictionaries
= We can create DataFrame from a list of Dictionaries.
"Here, the dictionary keys are taken as column labels, and the values corresponding to each
key are taken as rows.
= There will be as many rows as the number of dictionaries present in the list.
60| Page C.B. Patel Computer College24.25 Python Programming Language
= Number of columns in a DataFrame is equal to the maximum number of keys in any
dictionary of the list.
= NaN (Not a Number) is inserted if a corresponding value for a column is missing.
import pandas as pd
studList = [
{"Rol1No":1, "Name"
("Rol1No":2, "Name"
("Rol1No":3, "Name"
‘Akash", "Age":21,"Per":85.6, "Total":535},
Chirag", "Age":20, "Per":75.2},
Reena", "Age":19,"Per":63.4, "Total":535},
Isha", "Age":21, "Per'
("Rol 1No":4, "Name"
{"Rol1No":5, "Name": "Tisha", "Age":21,"
{"Rol1No":6, "Name": "Nisha", "Age":21,"
1
df = pd.DataFrame (studList)
print (df)
Lists = [{"RolltWo': 1, "Wane": "Akash", "Age": 21, 'Per': 65.61, {*RollNo': 2, "Name": "Chizag', "Age's 20,
Tel}, (ollto's 3, Mane": teams, ge" *: 63.4}, (RollNo': 4, Mane": "Isha, "Age's 21, 1
PollNo Mane Age
’ 1 wa o
1 2 Chirag 20
bo 1 ee
3
4 sta a
di.head() and df.tail()
= The method head() gives the first 5 rows and the method tail() returns the last'5 rows.
print(df-headt() # First 5 rows
print(df.head(3)) # First 3 rows
print(af-tai(3)) #last 3 rows
df.columns
= The above property is used to get a list having the names of all columns in a dataframe.
print(df.columns) # forthe above example
Index(["RollNo', 'Name', 'Age', 'Per', 'Total'], dtype='object')
df.shape
"The above property is used to get the shape (The number of rows and columns in a
dataframe)
print(df.shape) # gives (4,4) for the above example
Indexing and Selecting Data
= Indexing in pandas means simply selecting particular rows and columns of data from a
DataFrame,
61] Page C.B. Patel Computer College24.25 Python Programming Language
= Indexing means selecting all the rows and some of the columns, some of the rows and all of
the columns, or some of each of the rows and columns.
= Indexing can also be known as Subset Selection.
Pandas facilitates data selecting and indexing using three types of multi-a
1, Indexing operator [] and attribute operator .
2. Label-based indexing using .loc{)
3. Integer position-based indexing using :iloc{]
indexing:
Indexing a Dataframe using indexing operator [):
= Indexing operator [] is used to refer to the square brackets following an object.
Selecting a single column
= In order to select a single column, we simply put the name of the column in-between the
brackets{] . We cam also access column directly by its name as an attribute using the . (dot)
operator:
import pandas as pd o neaah,
df = pd.DataFrame(studList) 1 Bhavin
first = data[" Name") or first = df.Name 2 Ritesh
print(first) Name: Name, dtype: object
Selecting Multiple columns
= In order to select multiple columns, we simply we have to pass a list of columns in an
indexing operator.
first: = data[["Nane", "Per"]]
Name Per
0 Akash 95.6
1 Bhavin 85.9
2 Ritesh 75.9
‘Select Rows For Specific Column Value or on condition
= For example, if in the above csv file, we want to select all the rows where Age == 21.
Rol1No Name Per Age
0 1 Akash 95.6 21
= 3 Ritesh 75.9 21
Select Rows For Specific Column Value or on condition
= For example, if in the above csv file, we want to select all the rows where Per column >
70.00.
print(dfldf["Per"]>70.0])
RollNo Name Per Age
0 Z Akash 95.6 ba
1 2 Bhavin 85.9 22
2 3 Ritesh 75.9 21
Retrieving rows using Slicing
+ To select multiple rows, we can use slicing operator on dataframe.
62]
ge C.B. Patel Computer College24.25 Python Programming Language
import pandas as pd
df= pd.DataFrame(studList)
print(df{3:5]) #Will select row index 3 to 4 (5 will be excluded)
Rol1No Name Age Per Total
3 4 Isha 21 #67.7 NaN
4 5 Tisha 21 =#77.7 NaN
import pandas as pd
df = pd.DataFrame(studList)
print(df[0:5]) # Will select row index 0 to 4 (5 will be excluded)
print(df{:}) # Will select all rows
2. Indexing and Selections From Pandas Dataframes using loc[]:
+ locis label-based, which means that you have to specify rows and columns based on their
row and column labels.
‘= Itcan select a subset of rows and columns.
+ Joc{] takes two single/list/range operator separated by , . The first one indicates the row and
the second one indicates columns.
df.loc[
Sociedad Select Columns by Names/Labels
= START is the name of the row/column label
= STOP is the name of the last row/column label to take, and
= STEP as the number of indices to advance after each extraction
For loc[], if the label is not present it gives a key error.
Select a Single Row
import pandas as pd
studl
{"RolINo":1,"Name":"Akash","Age":21,"Per":85.6, "Total”:535},
{"RolINo":2,"Name":"Chirag","Age":20,"Per":75.2},
{"RolINo":3,"Name":"Reena’,"Age":19,"Per":63.4, "Total":535},
{"RolINo":4,"Name":"Isha”,"Age":21,"Per":67.7},
{"RolINo":5,"Name”:"Tisha","Age”:21,"Per":77.7},
{"RollNo":6,"Name”:"Nisha","Age":21,"Per":57.7}
]
df = pd.DataFrame(studlist)
print(df)
print("Selected Row =\n",df.loc(2])
+#Selecting a single row using row-Name (Here Index ~ 2)
Here 0,1, 2 upto 5 represents Row Index.
63| Pace C.B. Patel Computer College24-25 Python Programming Language
Original Data Frame Selected Single Row
RollNo Name Age Per Total aaa ae
° 1 Akash 21 85.6 535.0 ae
a 2 Chirag 20 75.2 NaN —
2 3 Reena 19 63:4 535.0 -
3 4 “Taha 21 67-7 NaN = et
4 5 Tisha 21 77.7 NaN ee ete
5 6 Nisha 21 57.7 NaN a ee
= We can set index on a different column using df.set_index() method.
Selected Row =
RollNo 5.0
Age 21.0
Per mt
Total NaN
Name: Tisha, dtype: floated |
j
Select Multiple Rows A \
= To select multiple rows for eg. Re nar will return rows 3,4 and 5.
Selected Row =
RollNo Name Age Per Total }
3 4 Isha 21 67.7 NaN }
4 5 Tisha 21 77.7 NaN
5
6 Nisha 21 57.7 NaN =
To select multiple rows, “Akash” and “Chirag” records we use
RollNo Age Per Tota:
Name A
Akash a 2) 65.6 5950 oe
Chirag 2 20 75.2 _ Nan™
Select Multiple Rows and Particular Columns
For selecting multiple rows and particular columns mention the row names and columns names
as giver
‘Syntax: Dataframe.loc[[“row!", “row2"..., [“column1”, “column2”, “column3'
Eg. "Select only Age and Per for Akash and Chirag”
Apreiscleeted Row =i nl Tas Cag age | ected ROW
Age Per
Name
Akash 2185.6
Chirag 20 75.2
Select all the Rows With Some Particular Columns
Gal Page C.B. Patel Computer College24.25 Python Programming Language
Eg. “Select Name and Age for all rows”
print("Selected Row = \n",df.loc{ : ,["Name","Age"]])
Name Age
0 Akash = 21
1 Chirag 20
2 Reena 19
3. Isha 21
4 Tisha 21
5 Nisha 21
Using Conditions with pandas loc{}
= When we want to select rows based on multiple conditions use the Pandas loc{] attribute.
= We can combine these conditions using relational operators, logical operators
ce & (and), | (or), and parentheses for grouping,
print(dfloc{dfl'Age'|==21])_ # Will select all records where Age = 21
RollNo Name Age Per Total
1 Akash 21 85.6 535.0
4 Isha 21 67.7 NaN
5 Tisha 21 77.7 NaN
6 Nisha 21 57.7 NaN
aRuwo
3. Integer position-based indexing using .iloc{]
= In the Python Pandas library, .iloc{] is an indexer used for integer-location-based indexing of
data in a DataFrame.
= It is a property that is used to select rows and columns by position/index. If the
osition/index does not exist, it gives an index error.
* [tis a valuable tool for data manipulation and extraction based on numerical positions within
the DataFrame.
eee td a eee Sa eee
‘= START is the integer index of the row/column.
+ STOP is the integer index of the last row/column where you wanted to stop the selection,
and
‘= STEP as the number of indices to advance after each extraction.
Some point to note about iloc{].
= Bynot providing a start index, iloc[] selects from the first row/column.
= By not providing stop, iloc[] selects all rows/columns from the start index.
= Providing both start and stop, selects all rows/columns in between.
C.B. Patel Computer College24.25 Python Programming Language
Select a Single Row
import pandas as pd
5.6, "Total":535},
{"RolINo":2,"Name":"Chirag","Age":20,"Per":75.2},
{"RolINo":3,"Name":"Reena’,"Age":19,"Per":63.4, "Total":535},
{'RollNo":4,"Name" ge":21,"Per"67.7},
{"RollNo":5,"Name "Age":21,"Per":77.7},
{"RollNo":6,"Name”:"Nisha","Age":21,"Per":57.7}
]
df = pd.DataFrame(studlist)
print(df)
print("Selected Row = \n",dfiloef1})
+#Selecting a single row using integer row index—Here row 2)
Here 0,1, 2 upto 5 represents Row Index.
Original Data Frame Selected Single Row
RollNo Name Age Total
° 1 Akash 21 535.0 Ay Rott .
i 2 chirag 20 nan | Name Chirag
2 3 Reena 19 535.0 , Age 20
3 4 Isha 21 NaN” | Pex 75.2
4 5 Tisha 21 NaN Total NaN
5 6 Nisha 21 NaN | |Name: 1, dtype: object
Display Selected Columns for Selected Row
Syntax: df.iloc{row-index,[col-index1, col-index-2...]]
= In the above example, we display only name and per column for row-index 1(Name column
index is 1 and Per Column indexis 3).
print("Selected Columns for Selected Row = \n",dfiloc{1,(1,3]})
Name Chirag
Per 15.2
Name
= Using slicing: To select multiple rows for eg. Row index~name 3 : 5 will return rows 3 and 4.
print("Selected Row =\n",dfiloc{3:5]) #Here 5 will not be included
RollNo Name Age Per Total
3 4 Isha 21 67.7 NaN
4 5 Tisha 21 77.7 NaN
= To select multiple rows, with row-index records we use df.iloc{[row-index-1,row-index-2..]]
print(dfiloc{[1,5]})_ #Display row with index 1 and index 5
RollNo Name Age Per Total
i 2 Chirag 20 75.2 NaN
5 6 Nisha 21 (57.7 NaN
66|Page C.B. Patel Computer College24-25
Python Programming Language
Select Multiple Rows and Particular Columns
For selecting multiple rows and particular columins mention the row names and columns names
as given:
‘Syntax: Dataframe.iloc{{row-index1, row-index2,,
J, [col-index-1,col-index-2,..]
Eg. “Select only Row-index 1 and Row-index-3 and Col-index-1 (Name) and Col-Index-3 (Per)
print("Selected Row = \n",df.iloc({1,3],[1,3]])
Selected Row
Name Per
1 Chirag 75.2
5 tel 67.7
Select all the Rows With Some Particular Columns
Eg. “Select Col-index-1 (Name) and Col-Index-3 (Per)for all rows”
print("Selected Rows=\n",df.iloc{:,[1,3]])
Name Per
0 Akash 85.6
1 Chirag 75.2
2 Reena 63.4
3 Isha 67.7
4 Tisha 77.7
S Wishs 57.7
Using Conditions with pandas loc{]
= When we want to select rows based on multiple conditions use the Pandas loc{] attribute.
= We can combine these conditions using
relational operators, logical operators
like & (and), | (07), and parentheses for grouping.
= But Boolean indexing cannot be performed directly. So we can do it in the following way
print(dfilocflist(df{'Age'}==21)]) # Will select all records where Age = 21
RollNo Name Age Per Total
0) 1 Akash 21 88.8 535.0
a 4 Isha 21 67.7 NaN
4 S Tighe 21 2 Bat
5 6 Nisha 21 57.7 NaN
loc [} ilocf]
Label-based data selector
Index-based data selector
Indices should be sorted in order, or locf } will
only select the mentioned indices when slicing
Indices should be numerical, else slicing cannot
be done.
Indices need not be sorted in order when slicing
Indices can be numerical or categorical
‘The end index is included during slicing
Accepts bool series or list in conditions
67 [Page C.B. Patel Com
The end index is excluded during slicing
‘Only accepts bool list in conditions
puter College24.25 Python Programming Language
What is a CSV file?
= CSV stands for “Comma Separated Values.”
= CSV is a file format you will frequently come across while working in the field of Data
Science.
= It is a type of text file that stores tabular data for better readability, easier understanding,
and faster processing.
= CSV files can be converted from a JSON file or created using Python or Java.
cbrervations /recerds
= We havea file named “Salary_Data.csv.”
= The first line of a CSV file is the header. It contains the names of the fields/features, which
are shown on top as the column names in the file.
= After the header, each line of the file is an observation/a record. The values of a record are
separated by “commas.”
Read CSV file using Pandas
Reading a CSV file using Pandas
To access data from the CSV file, we require a function read_esv{) from Pandas that retrieves
data in the form of the data frame:
= Before using this function, we must import the Pandas library, we will load the CSV file using
Pandas.
# Inport pandas
import pandas as pd
df = pd.read_csv("student.csv")
print (d#)
O/p:
Struct of CSV CSV File displayed using pandas
A 8 c D Rol1No Name Per Age
RolINo | Name Per Age 1 Akash 95.6 21
1
0
E [than Sa 2 Bhavin 85.9 22
4 2 3 Ritesh 75.9 21
C.B. Patel Computer College