Download as pdf or txt
Download as pdf or txt
You are on page 1of 44

AN

INDUSTRIAL TRAINING REPORT


ON

PYTHON (BASIC & ADVANCE)

Submitted in Partial Fulfillment for the Award of


Bachelor of Technology (B.Tech.) Degree
Of
Rajasthan Technical University, KOTA

Session: -2021-22

INDUSTRIAL TRAINING TAKEN AT “AZURE


SKYNET SOLUTIONS PVT LTD”
SUBMITTED TO:

DEPARTMENT OF ELECTRONICS & COMMUNICATION


ENGINEERING
ARYA COLLEGE OF ENGINEERING & I.T., JAIPUR-302028
(RAJASTHAN)

SUBMITTED BY:
Nirmal Mali
18EAREC058

1
CERTIFICATE

This is to certify that the INDUSTRIAL TRAINING report entitled “PYTHON (BASIC
& ADVANCE)” is submitted by NIRMAL MALI (18EAREC058), Final Year VII
semester in partial fulfillment of the degree of Bachelor of Technology in Electronics &
Communication Engineering of Rajasthan Technical University, Kota during the academic
year 2021-22. The work has been found satisfactory and is approved for submission.

Er. Manish Kumar Gupta

(TRAINING INCHARGE)

Dr. Rahul Srivastava


(HOD, Deptt. of ECE)

1
2
2
3
CANDIDATE’S DECLARATION

I hereby declare that the work, which is being presented in the Industrial training
Report, entitled “PYTHON (BASIC & ADVANCE)” is in partial fulfillment for the
requirement of the course of “Bachelor of Technology” in Deptt. Of Electronics &
Communication Engineering and submitted to the Department for a record of my own
studies carried under the guidance of Er. MANISH KUMAR GUPTA, Department of
Electronics & Communication Engineering, Arya College of Engineering & I.T. Jaipur.
I have not submitted the matter presented in the report anywhere for the requirement of
any other course.

NIRMAL MALI
18EAREC058
Arya College of Engineering & I.T. ,Kukas (Jaipur)

Er. MANISH KUMAR GUPTA

(TRAINING INCHARGE)

3
4
ACKNOWLEDGEMENT

I would like to extend my sincere gratitude to Mr. Manish Kumar Gupta (Seminar
Coordinator, Deptt. Of ECE) for this opportunity he had provided me. He has been a
great source of inspiration for all of us and his experience and knowledge have helped
us in learning and giving this seminar shape it was assumed.

I am indebted to Dr. Rahul Shrivastava (Head of ECE Department), for providing all
required resources for the completion of the seminar.

I express my thanks to all staff members and friends for all the help and coordination
and to the authors of references and other literature referred to in this seminar.

Last but not least, I am very much thankful to my parents who guided me in every step
which I took.

NIRMAL MALI
18EAREC058

4
5
ABSTRACT

The objective of a practical training is to learn something about industries practically and
to be familiar with the working style of a technical worker to adjust simply according to
the industrial environment . This report deals with the equipment , their relation, and
their general operating principle . Python , an interpreted language which was developed
by Guido van Rossum came into implementation in 1989. The language supports both
object- oriented and procedure - oriented approaches . Python is designed to be a highly
extensible language .

Python works on the principle of " there is only one obvious way to do a task " rather
than " there is more than one way to solve a particular problem " . Python is very easy to
learn and implement .

The simpler syntax , uncomplicated semantics and approach with which Python has
been developed makes it very easier to learn . A large number of python
implementations and extensions have been developed since its inception . Training
Cover provides both six weeks as well as six months industrial training in Python . Python
is divided into two parts as " Core Python " and " Advance Python " .of Accordingly , all
the basic and advanced topics are discussed in both of the modules

5
6
Table Of Contents
Chapter - 1 9
Introduction To Python 9
1.1 Python 9
1.2 Scripting Language 9
1.3 Object-Oriented Programming Language 9
1.4 History 10
1.5 Behind the scene of Python 10

Chapter - 2 11
Data Type in Python 11
2.1 Data Types 11
2.2 Variables 11
2.3 Strings 12
2.4 Creating Strings 12
2.5 Python Operators 13
Arithmetic Operator 13
Comparison Operator 14
Logical Operator 15

Chapter - 3 16
Tuples and Lists 16
3.1 Tuples 16
3.2 Accessing Values in Tuples 16
3.3 Basic Tuples Operations 17
3.4 Built-in Tuple Functions 17
3.5 List 18
3.6 Accessing Values in Lists: 19
3. 7 Basic List Operation 20
3. 8 Built-in List Functions & Methods: 20

Chapter - 4 23
Loops 23
4.1 Loops 23
4. 2 Examples: 24
4. 3 Conditional Statements: 25
4. 4 Functions 27

Chapter - 5 29
Scope of Python 29
5.1 What Can We do With Python? 29
5.2 Who Uses Python Today? 29
5.3 Why Do People Use Python? 30

7
Chapter - 6 31
Numpy 31
6.1 Introduction to Numpy 31
6.2 Operations using NumPy 31
6.3 Numpy -the ndarray Object 31
6.4 Operations on Numpy array 32
6.4.1 Aithmatic Operation 32
6.4.2 Transposing 34
6.4.3 Sorting 34
6.4.4 Concatenating Arrays 34
6.4.5 Reshaping Arrays 35
6.4.6 Reshaping Arrays 35
6.5 Indexing and Slicing of NumPy array 36
6.5.1 Indexing 36
6.5.2 Slicing 37

Chapter - 7 38
Pandas Library 38
7.1 Introduction to Pandas 38
7.2 Data Structures in Pandas 38
7.2.1 Series 39
7.2.1.1 Creation of Series 39
7.2.1.2 Indexing of Series 40
7.2.1.3 Slicing of Series 40
7.2.2 DataFrame 41
7.2.2.1 Creation of DataFrame 42
7.2.2.2 Indexing of DataFrame 42
7.3 Advantages of Pandas 43

Chapter - 8 44
Final Project - OpenMF 44
8.1 Project Introduction 44
8.2 Project Architecture 44
8.3 Project Components 44
8.3 Database design diagram 45
8.4 Project Demo 45
8.5 Technology Used 46

8
FIGURE INDEX

Figure 1:- Loops in Python 23


Figure 2:- Conditions in Python 25
Figure 3:- Numpy Array Objects 32
Figure 4:- Project Architecture 44
Figure 5:- Project Component 44
Figure 6:- Database Design Diagram 45
Figure 7:- Project Demo - 1 45
Figure 8:- Project Demo - 2 46
Figure 9:- Project Demo - 3 46

9
Chapter - 1

Introduction To Python

1.1 Python
Python is a widely used high-level, general-purpose, interpreted, dynamic programming language. Its
design philosophy emphasizes code readability, and its syntax allows programmers to express concepts
in fewer lines of code than would be possible in languages such as C++ or Java. The language provides
constructs intended to enable clear programs on both a small and large scale.
Python supports multiple programming paradigms, including object-oriented, imperative, and
functional programming or procedural styles. It features a dynamic type system and automatic memory
management and has a large and comprehensive standard library. Python interpreters are available for
installation on many operating systems, allowing Python code execution on a wide variety of systems.

1.2 Scripting Language


A scripting or script language is a programming language that supports scripts, programs written for a
special run-time environment that automate the execution of tasks that could alternatively be executed
one-by-one by a human operator.
Scripting languages are often interpreted (rather than compiled). Primitives are usually the elementary
tasks or API calls, and the language allows them to be combined into more complex programs.
Environments that can be automated through scripting include software applications, web pages within
a web browser, the shells of operating systems (OS), embedded systems, as well as numerous games.
A scripting language can be viewed as a domain-specific language for a particular environment; in the
case of scripting an application, this is also known as an extension language. Scripting languages are
also sometimes referred to as very high-level programming languages, as they operate at a high level
of abstraction, or as control languages.

1.3 Object-Oriented Programming Language

Object-oriented programming (OOP) is a programming paradigm based on the concept of "objects",


which may contain data, in the form of fields, often known as attributes; and code, in the form of
procedures, often known as methods. A distinguishing feature of objects is that an object's procedures

10
can access and often modify the data fields of the object with which they are associated (objects have a
notion of "this" or "self").

In OO programming, computer programs are designed by making them out of objects that interact
with one another. There is significant diversity in object-oriented programming, but most popular
languages are class-based, meaning that objects are instances of classes, which typically also
determines their type.

1.4 History

Python was conceived in the late 1980s, and its implementation was started in December 1989 by
Guido van Rossum at CWI in the Netherlands as a successor to the ABC language (itself inspired by
SETL) capable of exception handling and interfacing with the Amoeba operating system. Van Rossum
is Python's principal author, and his continuing central role in deciding the direction of Python is
reflected in the title given to him by the Python community, benevolent dictator for life (BDFL).

“Python is an experiment in how much freedom programmers need. Too much freedom and nobody
can read another's code; too little and expressiveness is endangered.”

1.5 Behind the scene of Python

About the origin of Python, Van Rossum wrote in 1996:


Over six years ago, in December 1989, I was looking for a "hobby" programming project that would
keep me occupied during the week around Christmas. My office ... would be closed, but I had a home
computer and not much else on my hands. I decided to write an interpreter for the new scripting
language I had been thinking about lately: a descendant of ABC that would appeal to Unix/C hackers. I
chose Python as a working title for the project, being in a slightly irreverent mood (and a big fan of
Monty Python's Flying Circus).

10

11
Chapter - 2

Data Type in Python

2.1 Data Types

Data types determine whether an object can do something, or whether it just would not make sense.
Other programming languages often determine whether an operation makes sense for an object by
making sure the object can never be stored somewhere where the operation will be performed on the
object (this type of system is called static typing). Python does not do that. Instead, it stores the type of
an object with the object, and checks when the operation is performed whether that operation makes
sense for that object (this is called dynamic typing).

Python has many native data types. Here are the important ones:

● Booleans are either True or False.


● Numbers can be integers (1 and 2), floats (1.1 and 1.2), fractions (1/2 and 2/3), or even
complex numbers.
● Strings are sequences of Unicode characters, e.g. an HTML document.
● Bytes and byte arrays, e.g. a JPEG image file.
● Lists are ordered sequences of values.
● Tuples are ordered, immutable sequences of values.
● Sets are unordered bags of values.

2.2 Variables

Variables are nothing but reserved memory locations to store values. This means that when you create
a variable you reserve some space in memory.
Based on the data type of a variable, the interpreter allocates memory and decides what can be stored
in the reserved memory. Therefore, by assigning different data types to variables, you can store
integers, decimals or characters in these variables.
Ex:
counter = 100 # An integer assignment
miles = 1000.0 # A floating point
11

12
name = "John" # A string

2.3 Strings

In programming terms, we usually call text a string. When you think of a string as a collection of
letters, the term makes sense.

All the letters, numbers, and symbols in this book could be a string.

For that matter, your name could be a string, and so could your address.

2.4 Creating Strings

In Python, we create a string by putting quotes around the text. For example, we could take our
otherwise useless

• "hello"+"world" "helloworld" # concatenation

• "hello"*3 "hellohellohello" # repetition

• "hello"[0] "h" # indexing

• "hello"[-1] "o" # (from end)

• "hello"[1:4] "ell" # slicing

• len("hello") 5 # size

• "hello" < "jello" 1 # comparison

• "e" in "hello" 1 # search

12

13
2.5 Python Operators

Arithmetic Operator

Operator Meaning Example

+ Add two operands or unary plus x+y

+2

- Subtract right operand from the left or unary x-y


minus
-2

* Multiply two operands x*y

/ Divide left operand by the right one (always x/y


results into float)

% Modulus - the remainder of the division of left x % y (remainder of


operand by the right x/y)

// Floor division - division that results into the whole x // y


number adjusted to the left in the number line

13

14
** Exponent - left operand raised to the power of x**y (x to the power
right y)

Comparison Operator

> Greater that - True if the left operand is greater than the right x>y

< Less that - True if the left operand is less than the right x<y

= Equal to - True if both operands are equal x ==


= y

!= Not equal to - True if operands are not equal x != y

> Greater than or equal to - True if the left operand is greater than or x >=
= equal to the right y

< Less than or equal to - True if the left operand is less than or equal to x <=
= the right y

14

15
Logical Operator

Operator Meaning Example

and True if both the operands are true x and y

or True if either of the operands is true x or y

not True if the operand is false (complements not x


the operand),

15

16
Chapter - 3

Tuples and Lists

3.1 Tuples

A tuple is a sequence of immutable Python objects. Tuples are sequences, just like lists. The
differences between tuples and lists are, the tuples cannot be changed, unlike lists and tuples that use
parentheses, whereas lists use square brackets.

Creating a tuple is as simple as putting different comma-separated values. Optionally you can put these
comma-separated values between parentheses also. For example −

tup1 = ('physics', 'chemistry', 1997, 2000);

tup2 = (1, 2, 3, 4, 5 );

tup3 = "a", "b", "c", "d";

3.2 Accessing Values in Tuples

To access values in tuple, use the square brackets for slicing along with the index or indices to obtain
value available at that index. For example −

tup1 = ('physics', 'chemistry', 1997, 2000);

tup2 = (1, 2, 3, 4, 5, 6, 7 );

print "tup1[0]: ", tup1[0]

print "tup2[1:5]: ", tup2[1:5]

When the above code is executed, it produces the following result −

tup1[0]: physics

tup2[1:5]: [2, 3, 4, 5]

16

17
3.3 Basic Tuples Operations

Tuples respond to the + and * operators much like strings; they mean concatenation and repetition here
too, except that the result is a new tuple, not a string. In fact, tuples respond to all of the general
sequence operations we used on strings in the prior chapter −

Python Expression Results Description

len((1, 2, 3)) 3 Length

(1, 2, 3) + (4, 5, 6) (1, 2, 3, 4, 5, 6) Concatenation

('Hi!',) * 4 ('Hi!', 'Hi!', 'Hi!', 'Hi!') Repetition

3 in (1, 2, 3) True Membership

for x in (1, 2, 3): print x, 123 Iteration

3.4 Built-in Tuple Functions

Python includes the following tuple functions −

SN Function with Description

17

18
1 cmp(tuple1, tuple2) Compares elements of both tuples.

2 len(tuple) Gives the total length of the tuple.

3 max(tuple) Returns item from the tuple with max value.

4 min(tuple) Returns item from the tuple with min value.

5 tuple(seq) Converts a list into tuple.

3.5 List

The list is a most versatile datatype available in Python which can be written as a list of
comma-separated values (items) between square brackets. Important thing about a list is that items in a
list need not be of the same type.

Creating a list is as simple as putting different comma-separated values between square brackets. For
example −

list1 = ['physics', 'chemistry', 1997, 2000];

list2 = [1, 2, 3, 4, 5 ];

list3 = ["a", "b", "c", "d"];

Similar to string indices, list indices start at 0, and lists can be sliced, concatenated and so on.

18

19
3.6 Accessing Values in Lists:

To access values in lists, use the square brackets for slicing along with the index or indices to obtain
value available at that index. For example −

list1 = ['physics', 'chemistry', 1997, 2000];

list2 = [1, 2, 3, 4, 5, 6, 7 ];

print "list1[0]: ", list1[0]

print "list2[1:5]: ", list2[1:5]

Output:list1[0]: physics

list2[1:5]: [2, 3, 4, 5]

Update: list = ['physics', 'chemistry', 1997, 2000];

print "Value available at index 2 : "

print list[2]

list[2] = 2001;

print "New value available at index 2 : "

print list[2]

Output:Value available at index 2 : 1997

New value available at index 2 : 2001

Delete: list1 = ['physics', 'chemistry', 1997, 2000];

print list1

del list1[2];

print "After deleting value at index 2 : "

19

20
print list1

['physics', 'chemistry', 1997, 2000]

Output:After deleting value at index 2 :

['physics', 'chemistry', 2000]

3. 7 Basic List Operation

Python Expression Results Description

len([1, 2, 3]) 3 Length

[1, 2, 3] + [4, 5, 6] [1, 2, 3, 4, 5, 6] Concatenation

['Hi!'] * 4 ['Hi!', 'Hi!', 'Hi!', 'Hi!'] Repetition

3 in [1, 2, 3] True Membership

for x in [1, 2, 3]: print x, 123 Iteration

3. 8 Built-in List Functions & Methods:

SN Function with Description

20

21
1 cmp(list1, list2) Compares elements of both lists.

2 len(list) Gives the total length of the list.

3 max(list) Returns item from the list with max value.

4 min(list) Returns item from the list with min value.

5 list(seq) Converts a tuple into list.

Python includes following list methods

SN Methods with Description

1 list.append(obj) Appends object obj to list

2 list.count(obj) Returns count of how many times obj occurs in list

3 list.extend(seq) Appends the contents of seq to list

4 list.index(obj) Returns the lowest index in list that obj appears

21

22
5 list.insert(index, obj) Inserts object obj into list at offset index

6 list.pop(obj=list[-1]) Removes and returns last object or obj from list

7 list.remove(obj) Removes object obj from list

8 list.reverse() Reverses objects of list in place

9 list.sort([func]) Sorts objects of list, use compare func if given

22

23
Chapter - 4

Loops

4.1 Loops

Programming languages provide various control structures that allow for more complicated execution
paths.

A loop statement allows us to execute a statement or group of statements multiple times. The following
diagram illustrates a loop statement −

Python programming language provides the following types of loops to handle looping requirements.

Loop Type Description

23

24
while loop Repeats a statement or group of statements while a given
condition is TRUE. It tests the condition before executing
the loop body.

for loop Executes a sequence of statements multiple times and


abbreviates the code that manages the loop variable.

nested loops You can use one or more loops inside any another while, for,
or do..while loop.

4. 2 Examples:

For Loop:

>>> for mynum in [1, 2, 3, 4, 5]:

print "Hello", mynum

Hello 1

Hello 2

Hello 3

Hello 4

Hello 5

While Loop:

>>> count = 0

24

25
>>> while (count < 4):

print 'The count is:', count

count = count + 1

The count is: 0

The count is: 1

The count is: 2

The count is: 3

4. 3 Conditional Statements:

Decision making is the anticipation of conditions occurring while execution of the program and
specifying actions taken according to the conditions.

Decision structures evaluate multiple expressions which produce TRUE or FALSE as outcomes. You
need to determine which action to take and which statements to execute if the outcome is TRUE or
FALSE otherwise.

25

26
Python programming language provides the following types of decision making statements. Click the
following links to check their detail.

Statement Description

if statements An if statement consists of a boolean expression


followed by one or more statements.

if...else statements An if statement can be followed by an optional else


statement, which executes when the boolean
expression is FALSE.

nested if statements You can use one if or else if statement inside another if
or else if statement(s).

Example:

If Statement:

>>> state = “Texas”

>>> if state == “Texas”:

print “TX

TX

If...Else Statement:

>>> if state == “Texas”

print “TX”

26

27
else:

print “[inferior state]”

If...Else...If Statement:

>>> if name == “Paige”

print “Hi Paige!”

elif name == “Walker”:

print “Hi Walker!”

else:

print “Imposter!”

4. 4 Functions

Function blocks begin with the keyword def followed by the function name and parentheses ( ( ) ).

Any input parameters or arguments should be placed within these parentheses. You can also define
parameters inside these parentheses.

The first statement of a function can be an optional statement - the documentation string of the
function.

The code block within every function starts with a colon (:) and is indented.

The statement return [expression] exits a function, optionally passing back an expression to the caller.
A return statement with no arguments is the same as a returof n None.

Syntex:

def functionname( parameters ):

"function_docstring"

function_suite

27

28
return [expression]

Example:

1. def printme( str ):

"This prints a passed string into this function"

print str

return

# Function definition is here

def printme( str ):

"This prints a passed string into this function"

print str

return;

# Now you can call printme function

printme("I'm first call to user defined function!")

printme("Again second call to the same function")

28

29
Chapter - 5

Scope of Python

Science

- Bioinformatics

System Administration

-Unix

-Web logic

-Web sphere

Web Application Development

-CGI

Testing scripts

5.1 What Can We do With Python?

● System programming
● Graphical User Interface Programming
● Internet Scripting
● Component Integration
● Database Programming
● Gaming, Images, XML , Robot and more

5.2 Who Uses Python Today?

● Python is being applied in real revenue-generating products by real companies.


● Google makes extensive use of Python in its web search system, and employs Python’s creator.

29

30
● Intel, Cisco, Hewlett-Packard, Seagate, Qualcomm, and IBM use Python for hardware testing.
● ESRI uses Python as an end-user customization tool for its popular GIS mapping products.
● The YouTube video sharing service is largely written in Python.

5.3 Why Do People Use Python?

The following primary factors cited by Python users

seem to be these:

· Python is object-oriented

o Structure supports such concepts as polymorphism, operation overloading, and


multiple inheritance.

· Indentation

o Indentation is one of the greatest future in Python.

· It's free (open source)

o Downloading and installing Python is free and easy

o Source code is easily accessible

· It's powerful

o Dynamic typing

o Built-in types and tools

o Library utilities

o Third party utilities (e.g. Numeric, NumPy, SciPy)

o Automatic memory management

· It's portable

o Python runs virtually every major platform used today

30

31
Chapter - 6

Numpy

6.1 Introduction to Numpy

NumPy is a Python package. It stands for 'Numerical Python. It is a library consisting of


multidimensional array objects and a collection of routines for processing of array. Numeric, the
ancestor of NumPy, was developed by Jim Hugunin. Another package Numarray was also developed,
having some additional functionalities. In 2005, Travis Oliphant created the NumPy package by
incorporating the features of Numarray into the Numeric package. There are many contributors to this
open-source project.

6.2 Operations using NumPy

Using Numpy a developer can perform the following operations: Mathematical and logical operations
on arrays. Fourier transforms and routines for shape manipulation. Operations related to linear
algebra. NumPy has in-built functions for linear algebra and random number generation.

6.3 Numpy -the ndarray Object

The most important object defined in NumPy is an N-dimensional array type called ndarray. It
describes the collection of items of the same type. Items in the collection can be accessed using a
zero-based index. Every item in an ndarray takes the same size of the block in the memory. Each
element in ndarray is an object of the data-type object (called dtype). Any item extracted from ndarray
object (by slicing) is represented by a Python object the t of one of array scalar types. The following
diagram shows a relationship between ndarray, data type object (dtype) and array scalar type:

31

32
An instance of ndarray class can be constructed by different array creation routines described later in
the tutorial. The basic ndarray is created using an array function in NumPy as follows:

>> numpy.array

It creates an ndarray from any object exposing array interface, or from any method that returns an
array.

>> numpy.array(object, dtype=None, copy=True, order=None, subok=False, ndmin=0)

6.4 Operations on Numpy array

6.4.1 Aithmatic Operation

Arithmetic operations on NumPy arrays are fast and simple. When we perform a basic arithmetic
operation like addition, subtraction, multiplication, division etc. on two arrays, the operation is done on
each corresponding pair of elements. For instance, adding two arrays will result in the first element in
the first array to be added to the first element in the second array, and so on. Consider the following
element-wise operations on two arrays:

>>> array1 = np.array([[3,6],[4,2]])

32

33
>>> array2 = np.array([[10,20],[15,12]])
#Element-wise addition of two matrices
>>> array1 + array2
array([[13, 26], [19, 14]])

#Subtraction
>>> array1 - array2
array([[ -7, -14], [-11, -10]])

#Multiplication
>>> array1 * array2
array([[ 30, 120], [ 60, 24]])

#Matrix Multiplication
>>> array1 @ array2
array([[120, 132], [ 70, 104]])

#Exponentiation
>>> array1 ** 3
array([[ 27, 216], [ 64, 8]], dtype=int32)

#Division
>>> array2 / array1
array([[3.33333333, 3.33333333], [3.75 , 6. ]])

#Element wise Remainder of Division


#(Modulo)
>>> array2 % array1
array([[1, 2], [3, 0]], dtype=int32)

It is important to note that for element-wise operations, size of both arrays must be same. That is,
array1.shape must be equal to array2.shape.

33

34
6.4.2 Transposing

Transposing an array turns its rows into columns and columns into rows just like matrices in
mathematics.
#Transpose
>>> array3 = np.array([[10,-7,0, 20], [-5,1,200,40],[30,1,-1,4]])
>>> array3
array([[ 10, -7, 0, 20], [ -5, 1, 200, 40], [ 30, 1, -1, 4]])

# the original array does not change


>>> array3.transpose()
array([[ 10, -5, 30], [ -7, 1, 1], [ 0, 200, -1], [ 20, 40, 4]])

6.4.3 Sorting

Sorting is to arrange the elements of an array in hierarchical order either ascending or descending. By
default, numpy does sorting in ascending order.

>>> array4 = np.array([1,0,2,-3,6,8,4,7])


>>> array4.sort()
>>> array4 array([-3, 0, 1, 2, 4, 6, 7, 8])

6.4.4 Concatenating Arrays

Concatenation means joining two or more arrays. Concatenating 1-D arrays means appending the
sequences one after another. NumPy.concatenate().
function can be used to concatenate two or more 2-D arrays either row-wise or column-wise. All the
dimensions of the arrays to be concatenated must match exactly except for the dimension or axis along
which they need to be joined. Any mismatch in the dimensions results in an error. By default, the
concatenation of the arrays happens along axis=0.

>>> array1 = np.array([[10, 20], [-30,40]])


>>> array2 = np.zeros((2, 3), dtype=array1. dtype)

34

35
>>> array1
array([[ 10, 20], [-30, 40]])

>>> array2
array([[0, 0, 0], [0, 0, 0]])

>>> array1.shape
(2, 2)
>>> array2.shape
(2, 3)

6.4.5 Reshaping Arrays

We can modify the shape of an array using the reshape() function. Reshaping an array cannot be used
to change the total number of elements in the array. Attempting to change the number of elements in
the array using reshape() results in an error.
>>> array3 = np.arange(10,22)
>>> array3
array([10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21])

6.4.6 Reshaping Arrays

We can split an array into two or more subarrays. numpy.split() splits an array along the specified axis.
We cthe an either specify sequence of index values where an array is to be split; or we can specify an
integer N, that indicates the number of equal parts in which the array is to be split, as parameter(s) to
the NumPy.split() function. By default, NumPy.split() splits along axis = 0. Consider the array given
below:
>>> array4
array([[ 10, -7, 0, 20], [ -5, 1, 200, 40], [ 30, 1, -1, 4], [ 1, 2, 0, 4], [ 0, 1, 0, 2]])
# [1,3] indicate the row indices on which
# to split the array
>>> first, second, third = numpy split(array4, [1, 3])

35

36
# array4 is split on the first row and
# stored on the sub-array first
>>> first array([[10, -7, 0, 20]])

# array4 is split after the first row and


# upto the third row and stored on the
# sub-array second
>>> second
array([[ -5, 1, 200, 40], [ 30, 1, -1, 4]])

# the remaining rows of array4 are stored


# on the sub-array third
>>> third
array([[1, 2, 0, 4], [0, 1, 0, 2]])

6.5 Indexing and Slicing of NumPy array

6.5.1 Indexing

For 2-D arrays indexing for both dimensions start from 0, and each element is referenced through two
indexes i and j, where I represent the row number and j represents the column number.

Consider the above Table showing marks obtained by students in three different subjects. Let us create
an array called marks to store marks given in three subjects for four students given in this table. As
there are 4 students (i.e. 4 rows) and 3 subjects (i.e. 3 columns), the array will be called marks[4][3].
This array can store 4*3 = 12 elements. Here, marks[i,j] refers to the element at (i+1)th row and (j+1)th

36

37
column because the index values start at 0. Thus marks[3,1] is the element in the 4th row and second
column which is 72 (marks of Prasad in English).
# accesses the element in the 1st row in
# the 3rd column
>>> marks[0,2] 56
>>> marks [0,4]
index Out of Bound "Index Error".
Index 4 is out of bounds for axis with size 3

6.5.2 Slicing

Sometimes we need to extract part of an array. This is done through slicing. We can define which part
of the array to be sliced by specifying the start and end index values using [start: end] along with the
array name.

>>> array8
array([-2, 2, 6, 10, 14, 18, 22])

# excludes the value at the end index


>>> array8[3:5]
array([10, 14])

# reverse the array


>>> array8[ : : -1]
array([22, 18, 14, 10, 6, 2, -2])

37

38
Chapter - 7

Pandas Library

7.1 Introduction to Pandas

Python libraries contain a collection of built-in modules that allow us to perform many actions without
writing detailed programs for it. Each library in Python contains a large number of modules that one
can import and use. NumPy, Pandas, and Matplotlib are three well-established Python libraries for
scientific and analytical use. These libraries allow us to manipulate, transform and visualize data easily
and efficiently. NumPy, which stands for ‘Numerical Python’, is a library. I. It is a package that can be
used for numerical data analysis and scientific computing. NumPy uses a multidimensional array
object and has functions and tools for working with these arrays. Elements of an array stay together in
memory, hence, they can be quickly accessed. PANDAS (PANel DAta) is a high-level data
manipulation tool used for analyzing data. It is very easy to import and export data using the Pandas
library which has a very rich set of functions. It is built on packages like NumPy and Matplotlib and
gives us a single, convenient place to do most of our data analysis and visualization work. Pandas have
three important data structures, namely – Series, DataFrame, and Panel to make the process of
analyzing data organized, effective and efficient.

7.2 Data Structures in Pandas


A data structure is a collection of data values and operations that can be applied to that data. It enables
efficient storage, retrieval, and modification of the data. For example, we have already worked with a
data structure ndarray in NumPy in Class XI. Recall the ease with which we can store, access, and
update data using a NumPy array. Two commonly used data structures in Pandas that we will cover in
this book are:
• Series
• DataFrame

38

39
7.2.1 Series

A Series is a one-dimensional array containing a sequence of values of any data type (int, float, list,
string, etc) which by default have numeric data labels starting from zero. The data label associated with
a particular value is called its index. We can also assign values of other data types as indexes. We can
imagine a Pandas Series as a column in a spreadsheet. An example of a series containing names of
students is given below:

7.2.1.1 Creation of Series

There are different ways in which a series can be created in Pandas. To create or use series, we first
need to import the Pandas library.
A Series can be created using scalar values as shown in the example below:

Observe that output is shown in two columns - the index is on the left and the data value is on the right.
If we do not explicitly specify an index for the data values while creating a series, then by default
indices range from 0 through N – 1. Here N is the number of data elements. We can also assign
user-defined labels to the index and use them to access elements of a Series. The following example
has a numeric index in random order.

39

40
Here, data values Kavi, Shyam, and Ravi have index values 3, 5, and 1, respectively. We can also use
letters or strings as indices, for example,

7.2.1.2 Indexing of Series

Indexing in Series is similar to that for NumPy arrays and is used to access elements in a series.
Indexes are of two types: positional index and labeled index. The positional index takes an integer
value that corresponds to its position in the series starting from 0, whereas the labeled index takes any
user-defined label as an index.
The following example shows the usage of the positional index for accessing a value from a Series.

>>> seriesNum = pd.Series([10,20,30])


>>> seriesNum[2]
30

Here, the value 30 is displayed for the positional index 2. When labels are specified, we can use labels
as indices while selecting values from a Series, as shown below. Here, the value 3 is displayed for the
labelled index Mar.
>>> seriesMnths = pd.Series([2,3,4],index=["Feb ","Mar","Apr"])
>>> seriesMnths["Mar"]
3I

7.2.1.3 Slicing of Series

Sometimes, we may need to extract a part of a series. This can be done through slicing. This is similar
to slicing used with NumPy arrays. We can define which part of the series is to be sliced by specifying

40

41
the start and end parameters [start: end] with the series name. When we indices for slicing, the value at
the endindex position is excluded, i.e., only (end - start) number of data values of the series are
extracted. Consider the following series seriesCapCntry:

As we can see in the above output, only data values at indices 1 and 2 are displayed. If labeled indexes
are used for slicing, then the value at the end index label is also included in the output, for example,

7.2.2 DataFrame

Sometimes we need to work on multiple columns at a time, i.e., we need to process the tabular data.
For example, the result of a class, items in a restaurant’s menu, reservation chart of a train, etc. Pandas
store such tabular data using a DataFrame. A DataFrame is a two-dimensional labeled 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.

41

42
7.2.2.1 Creation of DataFrame

There is a number of ways to create a DataFrame. Some of them are listed in this section.
Consider the following three NumPy ndarrays. Let us create a simple DataFrame without any column
labels, using a single ndarray:

7.2.2.2 Indexing of DataFrame

We can use slicing to select a subset of rows and/or columns from a DataFrame. To retrieve a set of
rows, slicing can be used with row labels. For example:
42

43
Here, the rows with labels Maths and Science are displayed. Note that in DataFrames slicing is
inclusive of the end values. We may use a slice of labels with a column name to access values of those
rows in that column only. For example, the following statement displays the rows with the label Maths
and Science, and the column with the label Arnab:

We may use a slice of labels with a slice of column names to access values of those rows and columns:

7.3 Advantages of Pandas

1. Data representation
2. Less writing and more work done
3. An extensive set of features
4. Efficiently handles large data
5. Makes data flexible and customizable
6. Fast as build upon NumPy.

43

44

You might also like