Unit 4 - 1
Unit 4 - 1
UNIT-IV
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 1
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 2
List Comprehensions in
Python
List comprehension is an elegant way to define and create a list in python.
We can create lists just like mathematical statements and in one line only.
The syntax of list comprehension is easier to grasp. A list comprehension
generally consists of these parts :
• Output expression,
• Input sequence,
• A variable representing a member of the input sequence and
• An optional predicate part.
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 3
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant. Prof. U1.1
MCA-106, Python Programming
List Comprehension
Example -1:
Lst = [x**2 for x in range(1,11) if x% 2 == 1]
In the above example,
• x ** 2 is the expression.
• range (1, 11) is input sequence or another list.
• x is the variable.
• if x % 2 == 1 is predicate part.
This is the power of list comprehension. It can identify when it receives a
string or a tuple and work on it like a list.
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 4
List Comprehension
Example -2 :
Suppose, we want to separate the letters of the word ‘human’ and add the
letters as items of a list. The first thing that comes in mind would be
using for loop.
h_letters = []
for letter in ‘human’:
h_letters.append(letter)
print(h_letters)
List Comprehension
In example-2, we can see that ‘human’ is a string, not a list. This is the
power of list comprehension. It can identify when it receives a string or a
tuple and work on it like a list.
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 6
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant. Prof. U1.2
MCA-106, Python Programming
Advantages of List
Comprehension
Advantages of List Comprehension
• More time efficient and space efficient than loops.
• Require fewer lines of code.
• Transforms iterative statement into a formula.
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 7
map() Function
The advantage of the lambda function can be seen when it is used in
combination with the map() function. map() is a function which takes two
arguments:
r = map(func, seq)
• The first argument func is the name of a function and the second a
sequence (e.g. a list) seq. map() applies the function func to all the
elements of the sequence seq.
• Before Python3, map() used to return a list, where each element of the
result list was the result of the function func applied on the
corresponding element of the list or tuple "seq". With Python 3, map()
returns an iterator.
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 8
map() Function
• The map() function executes a specified function for each item in an
iterable. The item is sent to the function as a parameter.
Example
Calculate the length of each word in the tuple:
Def myFunc(s):
return(len(s))
X = map(myFunc,(‘Apple’,’Banana’,’kiwi’))
Print(list(X))
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 9
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant. Prof. U1.3
MCA-106, Python Programming
filter() Function
filter(func, seq)
• It offers an elegant way to filter out all the elements of a
sequence "seq", for which the function func returns True. i.e. an
item will be produced by the iterator result of filter(func, seq) if
item is included in the sequence "seq" and if func(item) returns
True.
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 10
filter() Function
In the following example, we filter out first the odd and then the even
elements of the sequence of the first 11 Fibonacci numbers:
fibonacci = [0,1,1,2,3,5,8,13,21,34,55]
odd_num = list(filter(lambda x: x%2, fibonacci ))
Print(odd_num)
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 11
reduce() Function
The reduce(fun,seq) function is used to apply a particular function
passed in its argument to all of the list elements mentioned in the
sequence passed along. This function is defined in “functools” module.
It performs a rolling-computation as specified by the passed function to
the neighboring elements, by taking a function and an iterable as
arguments, and returns the final computed value.
Working :
1. At first step, first two elements of sequence are picked and the result
is obtained.
2. Next step is to apply the same function to the previously attained
result and the number just succeeding the second element and the
result is again stored.
3. This process continues till no more elements are left in the container.
4. The final returned result is returned and printed on console.
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 12
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant. Prof. U1.4
MCA-106, Python Programming
reduce() Function
Example:
from functools import reduce
# Returns the sum of two elements
def sumTwo(a,b):
return a+b
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 13
Comparison of Lambda
and List Comprehension
• List Comprehension is used to create lists, Lambdas are functions that
can process like other functions and thus return values or list.
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 14
NumPy Library
• NumPy, which stands for Numerical Python, is a library consisting of
multidimensional array objects and a collection of routines for
processing those arrays.
• NumPy is a general-purpose array-processing package. It provides a
high-performance multidimensional array object, and tools for working
with these arrays : the n-dimensional array. This is simple yet
powerful data structure.
• In Python we have lists that serve the purpose of arrays, but they are
slow to process. NumPy aims to provide an array object that is up to
50x faster than traditional Python lists.
• The array object in NumPy is called ndarray it provides a lot of
supporting functions that make working with ndarray very easy.
• Arrays are very frequently used in data science, where speed and
resources are very important.
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 15
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant. Prof. U1.5
MCA-106, Python Programming
NumPy Library
continued ….
• It is the fundamental package for scientific computing with Python. It
contains various features including these important ones:
• A powerful N-dimensional array object
• Sophisticated (broadcasting) functions
• Tools for integrating C/C++ and Fortran code
• Useful linear algebra, Fourier transform, and random number
capabilities
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 16
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 17
Arrays in NumPy
NumPy’s main object is the homogeneous multidimensional array.
• It is a table of elements (usually numbers), all of the same type,
indexed by a tuple of positive integers.
• In NumPy dimensions are called axes. The number of axes is rank.
• NumPy’s array class is called ndarray. It is also known by the
alias array.
• Items in the collection can be accessed using a zero-based index.
• Every item in an ndarray takes the same size of block in the memory.
Example:
import numpy
arr = numpy.array([1, 2, 3, 4, 5])
print(arr)
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 18
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant. Prof. U1.6
MCA-106, Python Programming
Array creation:
There are various ways to create arrays in NumPy.
• we can pass a list, tuple or any array-like object into the array()
method, and it will be converted into an ndarray.
General Syntax :
numpy.array(object, dtype = None, copy = True, order = None, subok = False, ndmin = 0)
1 object It represents the collection object. It can be a list, tuple, dictionary, set, etc.
2 dtype We can change the data type of the array elements by changing this option to the
specified type. The default is none.
3 copy It is optional. By default, it is true which means the object is copied.
4 order There can be 3 possible values assigned to this option. It can be C (column order), R
(row order), or A (any)
5 subok The returned array will be base class array by default. We can change this to make the
subclasses passes through by setting this option to true.
6 ndmin It represents the minimum dimensions of the resultant array.
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 19
Array creation:
Example:
import numpy as np
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 20
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant. Prof. U1.7
MCA-106, Python Programming
Dimensions in Arrays
0-D Arrays
• 0-D arrays, or Scalars, are the elements in an array. Each value in an
array is a 0-D array.
import numpy as np
arr = np.array(42)
print(arr)
1-D Arrays
• An array that has 0-D arrays as its elements is called uni-dimensional
or 1-D array.
• These are the most common and basic arrays.
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print(arr)
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 23
Dimensions in Arrays
2-D Arrays
• An array that has 1-D arrays as its elements is called a 2-D array.
• These are often used to represent matrix or 2nd order tensors.
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6]])
print(arr)
3-D arrays
• An array that has 2-D arrays (matrices) as its elements is called 3-D
array.
• These are often used to represent a 3rd order tensor.
import numpy as np
arr = np.array([[[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]])
print(arr)
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 24
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant. Prof. U1.8
MCA-106, Python Programming
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 25
newarr = arr.reshape(2, 2, 3)
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 27
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant. Prof. U1.9
MCA-106, Python Programming
numpy.empty
It creates an uninitialized array of specified shape and dtype. It uses the
following constructor −
numpy.empty(shape, dtype = float, order = 'C')
import numpy as np
x = np.empty([3,2], dtype = int)
print x
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 28
import numpy as np
a = np.arange(10)
s = slice(2,7,2)
print(a)
print a[s]
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 29
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant. Prof. U1.10
MCA-106, Python Programming
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 31
arr3d = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])
print(arr3d)
print(arr3d[0])
print(arr3d[1,0])
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 32
Array Operations
Scalar Addition
Scalars can be added and subtracted from arrays and arrays can be
added and subtracted from each other:
import numpy as np
a = np.array([1, 2, 3])
b=a+2
print(b)
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 33
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant. Prof. U1.11
MCA-106, Python Programming
Array Operations
Scalar Multiplication
NumPy arrays can be multiplied and divided by scalar integers and floats:
a = np.array([1,2,3])
b = 3*a
print(b)
a = np.array([10,20,30])
b = a/2
print(b)
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 34
Element-wise Operations
Arrays enable you to perform mathematical operations on whole blocks of
data using similar syntax to the equivalent operations between scalar
elements.
Example : Array-Operations.py
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 35
Element-wise Operations
(Unary functions)
Function Description
Compute the absolute value element-wise for integer, floating point, or complex values.
abs, fabs
Use fabs as a faster alternative for non-complex-valued data
sqrt Compute the square root of each element. Equivalent to arr ** 0.5
log, log10, log2, log1p Natural logarithm (base e), log base 10, log base 2, and log(1 + x), respectively
Compute the ceiling of each element, i.e. the smallest integer greater than or equal to
ceil
each element
Compute the floor of each element, i.e. the largest integer less than or equal to each
floor
element
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 36
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant. Prof. U1.12
MCA-106, Python Programming
Element-wise Operations
(Unary functions)
Example:
import numpy as np
arr = np.arange(10)
print(arr)
print(np.sqrt(arr))
print(np.exp(arr))
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 37
Element-wise Operations
(Binary universal functions)
Function Description
greater, greater_equal, less, less_equal, equal, Perform element-wise comparison, yielding boolean array.
not_equal Equivalent to infix operators >, >=, <, <=, ==, !=
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 38
Array Operations
numpy.reciprocal()
This function returns the reciprocal of argument, element-wise. For
elements with absolute values larger than 1, the result is always 0
because of the way in which Python handles integer division. For integer
0, an overflow warning is issued.
import numpy as np
a = np.array([0.25, 1.33, 1, 0, 100])
print(a )
print ('\n' )
print (np.reciprocal(a) )
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 39
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant. Prof. U1.13
MCA-106, Python Programming
Array Operations
numpy.power()
This function treats elements in the first input array as base and returns it
raised to the power of the corresponding element in the second input
array.
import numpy as np
a = np.array([10,100,1000])
print( a)
print (np.power(a,2) )
print ('\n' )
b = np.array([1,2,3])
print( b )
print ('\n')
print (np.power(a,b))
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 40
Array Operations
numpy.mod()
• This function returns the remainder of division of the corresponding
elements in the input array. The function numpy.remainder() also
produces the same result.
import numpy as np
a = np.array([10,20,30])
b = np.array([3,5,7])
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 41
The Python numpy aggregate functions are sum, min, max, mean,
average, product, median, standard deviation, variance, argmin, argmax,
percentile etc.
Example: Array-Aggregate-Functions.py
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 42
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant. Prof. U1.14
MCA-106, Python Programming
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 44
[[3 1 5 2]
[5 3 7 4]
[7 5 9 6]]
[[3 1 5]
[5 2 7]
[7 3 9]]
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 45
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant. Prof. U1.15
MCA-106, Python Programming
Example :
x = np.array([3, 5, 7])
y = np.array([5, 7, 9])
print(np.vstack((x,y)))
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 46
• The values will be appended at the end of the array and a new ndarray
will be returned with new and old values.
• The axis is an optional integer along which define how the array is
going to be displayed. If the axis is not specified, the array structure will
be flattened as you will see later.
a = np.array([1, 2, 3])
newArray = np.append (a, [10, 11, 12])
print(newArray)
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 47
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 48
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant. Prof. U1.16
MCA-106, Python Programming
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 49
a = np.array([1, 2, 3])
newArray = np.insert(a, 1, 90)
print(newArray)
Here the insert() method adds the element at index 1. Remember the
array index starts from 0.
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 50
a = np.array([1, 2, 3])
if(a.size == 0):
print("The given Array is empty")
else:
print("The array = ", a)
Check with
a = np.array([])
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 51
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant. Prof. U1.17
MCA-106, Python Programming
a = np.array([1, 2, 3, 4, 5])
print("5 is found at index: ", np.where(a == 5))
The where() method will also return the datatype. If you want to just get the
index, use the following code:
a = np.array([1, 2, 3, 4, 5])
index = np.where(a == 5)
print("5 is found at index: ", index[0])
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 52
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 53
Pandas Library
pandas is a fast, powerful, flexible and easy to use open source data
analysis and manipulation tool, built on top of the Python programming
language.
import pandas as pd
df = pd.read_csv('D:/Python Programming/Scripts/myArray.csv')
print(df.to_string())
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 54
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant. Prof. U1.18
MCA-106, Python Programming
Data -Frames
A DataFrame is a 2-dimensional data structure that can store data of
different types (including characters, integers, floating point values,
categorical data and more) in columns. It is similar to a spreadsheet, a
SQL table Column
Row
The table has 3 columns, each of them with a column label. The column
labels are respectively Name, Age and Gender.
The column Name consists of textual data with each value a string, the
column Age are numbers and the column Gender is textual data.
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 55
Data -Frames
df = pd.DataFrame(
{
"Name": [
"Braund, Mr. Owen Harris",
"Allen, Mr. William Henry",
"Bonnell, Miss. Elizabeth",
],
"Age": [22, 35, 58],
“Gender": ["male", "male", "female"],
}
)
print(df)
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 56
Data -Frames
Locate Row
Pandas use the loc attribute to return one or more specified row(s)
print(df.loc[0])
This returns a Pandas Series.
print(df.loc[[0, 1]])
When using [ ], the result is a Pandas DataFrame.
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 57
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant. Prof. U1.19
MCA-106, Python Programming
Data -Frames
Named Indexes
With the index argument, you can name your own indexes.
data = {
"calories": [420, 380, 390],
"duration": [50, 40, 45]
}
df = pd.DataFrame(data, index = ["day1", "day2", "day3"])
print(df)
print(df.loc["day2"])
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 58
Series
• A Pandas Series is like a column in a table.
• It is a one-dimensional array holding data of any type.
• A pandas Series has no column labels, as it is just a single column of
a DataFrame. A Series does have row labels.
• If nothing else is specified, the values are labeled with their index
number. First value has index 0, second value has index 1 etc.
• This label can be used to access a specified value.
• With the index argument, we can give labels.
a = [1, 7, 2]
myvar = pd.Series(a, index = ["x", "y", "z"])
print(myvar)
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 59
Series
When selecting a single column of a pandas DataFrame, the result is a
pandas Series. To select the column, use the column label in between
square brackets [].
Example :
df["Age"]
Or
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 60
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant. Prof. U1.20
MCA-106, Python Programming
Series
Key/Value Objects as Series
You can also use a key/value object, like a dictionary, when creating a
Series.
import pandas as pd
calories = {"day1": 420, "day2": 380, "day3": 390}
myvar = pd.Series(calories)
print(myvar)
The keys of the dictionary become the labels.
To select only some of the items in the dictionary, use the index argument
and specify only the items you want to include in the Series.
import pandas as pd
calories = {"day1": 420, "day2": 380, "day3": 390}
myvar = pd.Series(calories, index = ["day1", "day2"])
print(myvar)
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 61
1. Loading data
The first step for data preparation is to get some data. If you have a .csv
file, you can easily load it up in your system using the read_csv() function
in pandas. We can work with Data-frames and Series as well.
df = pd.read_csv('D:/Python Programming/Scripts/myArray.csv')
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 62
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant. Prof. U1.21
MCA-106, Python Programming
What you can do to really "drop" or delete the NaN value is either store the new
dataset (without NaN) so that the original data Series is not tampered or apply a
drop inplace. The inplace argument has a default value of false .
not_null_data = data.dropna()
print(not_null_data)
data.dropna(inplace = True)
print(data)
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 64
data_dim =
pd.DataFrame([[1,2,3,np.nan],[4,5,np.nan,np.nan],[7,np.nan,np.nan,np.nan],[np.nan
,np.nan,np.nan,np.nan]])
print(data_dim)
Now let's say we only want to drop rows or columns that are all null or
only those that contain a certain amount of null values.
Try data_dim.dropna() : It will not work and the real dataset is not
tampered.
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 65
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 66
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant. Prof. U1.22
MCA-106, Python Programming
DataFrame Indexing
• Indexing in pandas means simply selecting particular rows and
columns of data from a DataFrame.
• Indexing could mean 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.
• The Python and NumPy indexing operators "[ ]" and attribute operator
"." provide quick and easy access to Pandas data structures across a
wide range of use cases.
• But, since the type of the data to be accessed isn’t known in advance,
directly using standard operators has some optimization limits.
• We take advantage of some optimized pandas data access methods
like .loc(), .iloc(), .ix().
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 67
DataFrame Indexing
loc() Method: (Label Based)
• Pandas provide various methods to have purely label based indexing.
When slicing, the start bound is also included. Integers are valid labels,
but they refer to the label and not the position.
loc takes two single/list/range operator separated by ','. The first one
indicates the row and the second one indicates columns.
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 68
DataFrame Indexing
Example:
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randn(6, 4),
index = ['a','b','c','d','e','f'], columns = ['A', 'B', 'C','D'])
print (df.loc['a':'d'])
print (df.loc['a':'c','D'])
print (df.loc['a':'f','A':'B'])
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 69
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant. Prof. U1.23
MCA-106, Python Programming
DataFrame Indexing
.iloc() method:
• Pandas provide various methods in order to get purely integer based
indexing. Like python and numpy, these are 0-based indexing.
• It is primarily integer position based from 0 to length – 1 of the axis
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 70
DataFrame Indexing
Example:
import pandas as pd
import numpy as np
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 71
DataFrame Indexing
.ix() method:
• Besides pure label based and integer based, Pandas provides a hybrid
method for selections and subsetting the object using the .ix() operator.
• The .ix indexer is deprecated in all the version after 0.20.0, in favor of
the more strict .iloc and .loc indexers.
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 72
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant. Prof. U1.24
MCA-106, Python Programming
DataFrame Indexing
Select Data Using Columns
In addition to location-based and label-based indexing, you can also
select data from pandas dataframes by selecting entire columns using the
column names.
dataframe[“column”]
Above command provides the data from the column as a pandas series,
which is a one-dimensional array. A pandas series is useful for selecting
columns for plotting using matplotlib.
You can also specify that you want an output that is also a pandas
dataframe.
dataframe[[“column”]]
which includes a second set of brackets [ ], to indicate that the output
should be a pandas dataframe.
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 73
DataFrame Indexing
You can also select all data from multiple columns in a pandas dataframe
using:
dataframe[[“column1”, “column2”]]
Since the results of your selection are also a pandas dataframe, you can
assign the results to a new pandas dataframe.
Try This:
Use avg-precip-months.csv
create a new pandas dataframe that only contains the months and
seasons column effectively dropping the precip values.
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 74
DataFrame Indexing
Filter Data Using Specific Values
In addition to location-based and label-based indexing, you can select or
filter data based on specific values within a column using:
dataframe[dataframe[“column1”]==value]
This will return all rows containing that value within the specified column.
Again, you can also save the output to a new dataframe by setting it equal
to the output of the filter.
You can also filter using a comparison operator on numeric values.
Try This :
Select all rows that have a season value of summer.
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 75
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant. Prof. U1.25
MCA-106, Python Programming
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 76
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 77
# display
print(data1)
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 78
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant. Prof. U1.26
MCA-106, Python Programming
# display
print(data)
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 79
Data Visualization
Data visualization is the discipline of trying to understand data by placing
it in a visual context so that patterns, trends and correlations that might
not otherwise be detected can be exposed.
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 80
Data Visualization
Useful packages for visualizations in python
• Matplotlib
• Matplotlib is a visualization library in Python for 2D plots of arrays.
Matplotlib is written in Python and makes use of the NumPy library.
Matplotlib comes with a wide variety of plots like line, bar, scatter,
histogram, etc. which can help us, deep-dive, into understanding
trends, patterns, correlations. It was introduced by John Hunter in
2002.
• Seaborn
• Seaborn is a dataset-oriented library for making statistical
representations in Python. It is developed atop matplotlib and to create
different visualizations. It is integrated with pandas data structures. The
library internally performs the required mapping and aggregation to
create informative visuals It is recommended to use a Jupyter/IPython
interface in matplotlib mode.
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 81
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant. Prof. U1.27
MCA-106, Python Programming
Data Visualization
• Bokeh
• Bokeh is an interactive visualization library for modern web browsers. It
is suitable for large or streaming data assets and can be used to
develop interactive plots and dashboards. There is a wide array of
intuitive graphs in the library which can be leveraged to develop
solutions. It works closely with PyData tools. The library is well-suited
for creating customized visuals according to required use-cases.
• Altair
• Altair is a declarative statistical visualization library for Python. Altair’s
API is user-friendly and consistent and built atop Vega-Lite JSON
specification. Declarative library indicates that while creating any
visuals, we need to define the links between the data columns to the
channels (x-axis, y-axis, size, color). With the help of Altair, it is
possible to create informative visuals with minimal code.
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 82
Data Visualization
• plotly
• plotly.py is an interactive, open-source, high-level, declarative, and
browser-based visualization library for Python. It holds an array of
useful visualization which includes scientific charts, 3D graphs,
statistical charts, financial charts among others. Plotly graphs can be
viewed in Jupyter notebooks, standalone HTML files, or hosted online.
• ggplot
• ggplot is a Python implementation of the grammar of graphics. The
Grammar of Graphics refers to the mapping of data to aesthetic
attributes (colour, shape, size) and geometric objects (points, lines,
bars). The basic building blocks according to the grammar of graphics
are data, geom (geometric objects), stats (statistical transformations),
scale, coordinate system, and facet.
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 83
• Histogram
The histogram represents the frequency of occurrence of specific
phenomena which lie within a specific range of values and arranged in
consecutive and fixed intervals.
• Bar Charts
A bar chart can be created using the bar method. The bar-chart isn’t
automatically calculating the frequency of a category so we are going
to use pandas value_counts function to do this. The bar-chart is useful
for categorical data that doesn’t have a lot of different categories
(less than 30) because else it can get quite messy.
Example: Test-plot.py
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 84
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant. Prof. U1.28
MCA-106, Python Programming
• Pie Chart :
A pie chart shows a static number and how categories represent part of
a whole the composition of something. A pie chart represents numbers
in percentages, and the total sum of all segments needs to equal
100%.
• Scatter plot :
• A scatter chart shows the relationship between two different variables
and it can reveal the distribution trends. It should be used when there
are many different data points, and you want to highlight similarities in
the data set. This is useful when looking for outliers and for
understanding the distribution of your data.
Example: Piechart.py
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 85
plt.pie(y)
plt.show()
• As you can see the pie chart draws one piece (called a wedge) for
each value in the array (in this case [35, 25, 25, 15]).
• By default the plotting of the first wedge starts from the x-axis and
move counterclockwise:
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 86
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 87
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant. Prof. U1.29
MCA-106, Python Programming
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 88
plt.figure(figsize=(width, height))
plt.figure(figsize=(3, 3))
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 89
Plot() Method
import matplotlib.pyplot as plt
import numpy as np
plt.plot(xpoints, ypoints)
plt.plot(xpoints, ypoints, marker= 'o')
plt.show()
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 90
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant. Prof. U1.30
MCA-106, Python Programming
Plot() Method
Line Reference
Line Syntax Description
'-' Solid line
':' Dotted line
'--' Dashed line
'-.' Dashed/dotted line
plt.plot(ypoints, 'o:r')
plt.show()
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 91
Plot() Method
Color Reference
Color Syntax Description
'r' Red
'g' Green
'b' Blue
'c' Cyan
'm' Magenta
'y' Yellow
'k' Black
'w' White
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 92
Plot() Method
import matplotlib.pyplot as plt
import numpy as np
plt.plot(xpoints, ypoints)
plt.show()
If we do not specify the points in the x-axis, they will get the default values
0, 1, 2, 3, (etc. depending on the length of the y-points.
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 93
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant. Prof. U1.31
MCA-106, Python Programming
Multiple Lines
import matplotlib.pyplot as plt
import numpy as np
y1 = np.array([3, 8, 1, 10])
y2 = np.array([6, 2, 7, 11])
plt.plot(y1)
plt.plot(y2)
plt.show()
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 94
x = np.array([80, 85, 90, 95, 100, 105, 110, 115, 120, 125])
y = np.array([240, 250, 260, 270, 280, 290, 300, 310, 320, 330])
plt.plot(x, y)
plt.xlabel("Average Pulse")
plt.ylabel("Calorie Burnage")
plt.show()
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 95
font1 = {'family':'serif','color':'blue','size':20}
font2 = {'family':'serif','color':'darkred','size':15}
plt.plot(x, y)
plt.show()
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 96
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant. Prof. U1.32
MCA-106, Python Programming
#plot 1:
x = np.array([0, 1, 2, 3])
y = np.array([3, 8, 1, 10])
plt.subplot(1, 2, 1)
plt.plot(x,y)
#plot 2:
x = np.array([0, 1, 2, 3])
y = np.array([10, 20, 30, 40])
plt.subplot(1, 2, 2)
plt.plot(x,y)
plt.show()
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 97
Display subplots
The subplots() Function
• The subplot() function takes three arguments that describes the layout
of the figure.
• The layout is organized in rows and columns, which are represented by
the first and second argument.
• The third argument represents the index of the current plot.
plt.subplot(1, 2, 1)
#the figuree has 1 row, 2 columns, and this plot is the first plot.
plt.subplot(1, 2, 2)
#the figure has 1 row, 2 columns, and this plot is the second plot.
Try This: If we want a figure with 2 rows an 1 column
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 98
Example:
import matplotlib.pyplot as plt
import numpy as np
#plot 1:
x = np.array([0, 1, 2, 3])
y = np.array([3, 8, 1, 10])
plt.subplot(1, 2, 1)
plt.plot(x,y)
plt.title("SALES")
#plot 2:
x = np.array([0, 1, 2, 3])
y = np.array([10, 20, 30, 40])
plt.subplot(1, 2, 2)
plt.plot(x,y)
plt.title("INCOME")
plt.show()
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 99
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant. Prof. U1.33
MCA-106, Python Programming
GUI Programming
Most of the programs we have done till now are text-based programming.
But many applications need GUI (Graphical User Interface).
Python provides several different options for writing GUI based programs.
These are listed below:
• Tkinter: It is easiest to start with. Tkinter is Python's standard GUI
(graphical user interface) package. It is the most commonly used toolkit
for GUI programming in Python.
• JPython: It is the Python platform for Java that is providing Python
scripts seamless access o Java class Libraries for the local machine.
• wxPython: It is an open-source, cross-platform GUI toolkit written in
C++. It is one of the alternatives to Tkinter, which is bundled with
Python.
There are many other interfaces available for GUI. But these are the most
commonly used ones.
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 100
Tkinter
It is the standard GUI toolkit for Python. Fredrik Lundh wrote it. For
modern Tk binding, Tkinter is implemented as a Python wrapper for the Tcl
Interpreter embedded within the interpreter of Python. Tk provides the
following widgets:
• Button canvas
• combo-box frame
• Level check-button
• Entry level-frame
• Menu list - box
• menu button message
• tk_optoinMenu progress-bar
• radio button scroll bar
• Separator tree-view, and many more.
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 101
Tkinter
Steps to create GUI program using Tkinter:
• Import the module Tkinter
• Build a GUI application (as a window)
• Add those widgets that are discussed above
• Enter the primary, i.e., the main event's loop for taking action when the
user triggered the event.
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 102
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant. Prof. U1.34
MCA-106, Python Programming
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 103
There are a number of widgets which you can put in your tkinter
application.
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 104
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 105
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant. Prof. U1.35
MCA-106, Python Programming
Button
To add a button in your application, this widget is used.
The general syntax is:
w=Button(master, option=value)
master is the parameter used to represent the parent window.
There are number of options which are used to change the format of the
Buttons. Number of options can be passed as parameters separated by
commas. Some of them are listed below.
• activebackground: to set the background color when button is under the cursor.
• activeforeground: to set the foreground color when button is under the cursor.
• bg: to set he normal background color.
• command: to call a function.
• font: to set the font on the button label.
• image: to set the image on the button.
• width: to set the width of the button.
• height: to set the height of the button.
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 106
Button (Example)
import tkinter as tk
r = tk.Tk()
r.title('Counting Seconds')
button = tk.Button(r, text='Stop', width=25, command=r.destroy)
button.pack()
r.mainloop()
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 107
Canvas
It is used to draw pictures and other complex layout like graphics, text and
widgets. The general syntax is:
w = Canvas(master, option=value)
There are number of options which are used to change the format of the
widget. Number of options can be passed as parameters separated by
commas. Some of them are listed below.
• bd: to set the border width in pixels.
• bg: to set the normal background color.
• cursor: to set the cursor used in the canvas.
• highlightcolor: to set the color shown in the focus highlight.
• width: to set the width of the widget.
• height: to set the height of the widget.
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 108
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant. Prof. U1.36
MCA-106, Python Programming
Canvas (Example)
import tkinter as tk
master = tk.Tk()
w = tk.Canvas(master, width=40, height=60)
w.pack()
canvas_height=20
canvas_width=200
y = int(canvas_height / 2)
w.create_line(0, y, canvas_width, y )
w.mainloop()
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 109
CheckButton
To select any number of options by displaying a number of options to a
user as toggle buttons.
import tkinter
from tkinter import *
top = tkinter.Tk()
CheckVar1 = IntVar()
CheckVar2 = IntVar()
tkinter.Checkbutton(top, text = "Machine Learning",variable =
CheckVar1,onvalue = 1, offvalue=0).grid(row=0,sticky=W)
tkinter.Checkbutton(top, text = "Deep Learning", variable =
CheckVar2, onvalue = 0, offvalue =1).grid(row=1,sticky=W)
top.mainloop()
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 110
Entry
Entry:It is used to input the single line text entry from the user.. For multi-
line text input, Text widget is used.
There are number of options which are used to change the format of the
widget. Number of options can be passed as parameters separated by
commas. Some of them are listed below.
• bd: to set the border width in pixels.
• bg: to set the normal background color.
• cursor: to set the cursor used.
• command: to call a function.
• highlightcolor: to set the color shown in the focus highlight.
• width: to set the width of the button.
• height: to set the height of the button.
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 111
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant. Prof. U1.37
MCA-106, Python Programming
Entry (Example)
from tkinter import *
master = Tk()
Label(master, text='First Name').grid(row=0)
Label(master, text='Last Name').grid(row=1)
e1 = Entry(master)
e2 = Entry(master)
e1.grid(row=0, column=1)
e2.grid(row=1, column=1)
mainloop()
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 112
Frame
Frame: It acts as a container to hold the widgets. It is used for grouping
and organizing the widgets.
There are number of options which are used to change the format of the
widget. Number of options can be passed as parameters separated by
commas. Some of them are listed below.
• highlightcolor: To set the color of the focus highlight when widget has
to be focused.
• bd: to set the border width in pixels.
• bg: to set the normal background color.
• cursor: to set the cursor used.
• width: to set the width of the widget.
• height: to set the height of the widget.
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 113
Frame(Example)
from tkinter import *
root = Tk()
frame = Frame(root)
frame.pack()
bottomframe = Frame(root)
bottomframe.pack( side = BOTTOM )
redbutton = Button(frame, text = 'Red', fg ='red')
redbutton.pack( side = LEFT)
greenbutton = Button(frame, text = 'Brown', fg='brown')
greenbutton.pack( side = LEFT )
bluebutton = Button(frame, text ='Blue', fg ='blue')
bluebutton.pack( side = LEFT )
blackbutton = Button(bottomframe, text ='Black', fg ='black')
blackbutton.pack( side = BOTTOM)
root.mainloop()
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 114
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant. Prof. U1.38
MCA-106, Python Programming
Label
Label: It refers to the display box where you can put any text or image
which can be updated any time as per the code.
There are number of options which are used to change the format of the
widget. Number of options can be passed as parameters separated by
commas. Some of them are listed below.
• bg: to set he normal background color.
• bg to set he normal background color.
• command: to call a function.
• font: to set the font on the button label.
• image: to set the image on the button.
• width: to set the width of the button.
• height” to set the height of the button.
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 115
ListBox
Listbox: It offers a list to the user from which the user can accept any
number of options.
There are number of options which are used to change the format of the
widget. Number of options can be passed as parameters separated by
commas.
• highlightcolor: To set the color of the focus highlight when widget has
to be focused.
Lb = Listbox(root)
Lb.insert(1, 'Python')
Lb.insert(2, 'Java')
Lb.insert(3, 'C++')
Lb.insert(4, 'Any other')
Lb.pack()
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 116
MenuButton
It is a part of top-down menu which stays on the window all the time.
Every menubutton has its own functionality.
from tkinter import *
import tkinter
top = Tk()
mb = Menubutton ( top, text = "Menu Items", relief= RAISED)
mb.grid()
mb.menu = Menu ( mb, tearoff = 0 )
mb["menu"] = mb.menu
cVar = IntVar()
aVar = IntVar()
mb.menu.add_checkbutton ( label ='Contact', variable = cVar )
mb.menu.add_checkbutton ( label = 'About', variable = aVar )
mb.pack()
top.mainloop()
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 117
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant. Prof. U1.39
MCA-106, Python Programming
Menu
Menu: It is used to create all kinds of menus used by the application
from tkinter import *
root = Tk()
menu = Menu(root)
root.config(menu=menu)
filemenu = Menu(menu)
menu.add_cascade(label='File', menu=filemenu)
filemenu.add_command(label='New')
filemenu.add_command(label='Open...')
filemenu.add_separator()
filemenu.add_command(label='Exit', command=root.quit)
helpmenu = Menu(menu)
menu.add_cascade(label='Help', menu=helpmenu)
helpmenu.add_command(label='About')
mainloop()
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 118
Message
Message: It refers to the multi-line and non-editable text.
The message widget is similar in its functionality to the Label widget, but it
is more flexible in displaying text, e.g. the font can be changed while
the Label widget can only display text in a single font. It provides a
multiline object, that is the text may span more than one line
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 119
RadioButtons
The Radiobutton is a standard Tkinter widget used to implement one-of-
many selections. Radiobuttons can contain text or images, and you can
associate a Python function or method with each button. When the button
is pressed, Tkinter automatically calls that function or method.
General Syntax:
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 120
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant. Prof. U1.40
MCA-106, Python Programming
RadioButtons (Example)
from tkinter import *
def sel():
selection = "You selected the option " + str(var.get())
label.config(text = selection)
root = Tk()
var = IntVar()
R1 = Radiobutton(root, text="Option 1", variable=var, value=1, command=sel)
R1.pack( anchor = W )
Database Access
• Python can be used in database applications.
• One of the most popular databases is MySQL.
• You can download a free MySQL database
at https://www.mysql.com/downloads/.
• Python needs a MySQL driver to access the MySQL
database.
• use PIP to install "MySQL Connector".
• Use command python -m pip install mysql-connector-
python
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 122
Create Connection
• We can start by creating a connection to the database.
• Use the username and password from your MySQL
database:
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword"
)
print(mydb)
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 123
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant. Prof. U1.41
MCA-106, Python Programming
Creating a Database
To create a database in MySQL, use the "CREATE DATABASE"
statement:
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword"
)
mycursor = mydb.cursor()
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 124
Creating a Table
• To create a table in MySQL, use the "CREATE TABLE"
statement.
• Make sure you define the name of the database when you
create the connection
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="mydatabase"
)
mycursor = mydb.cursor()
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 125
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="mydatabase"
)
mycursor = mydb.cursor()
mydb.commit()
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant. Prof. U1.42
MCA-106, Python Programming
mycursor.executemany(sql, val)
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 127
mydb.commit()
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="mydatabase"
)
mycursor = mydb.cursor()
myresult = mycursor.fetchall()
for x in myresult:
print(x)
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 128
Selecting Columns
mycursor = mydb.cursor()
myresult = mycursor.fetchall()
for x in myresult:
print(x)
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 129
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant. Prof. U1.43
MCA-106, Python Programming
mycursor = mydb.cursor()
mycursor.execute(sql)
myresult = mycursor.fetchall()
for x in myresult:
print(x)
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 130
mycursor.execute(sql, adr)
myresult = mycursor.fetchall()
for x in myresult:
print(x)
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 131
Delete Record
You can delete records from an existing table by using the "DELETE
FROM" statement:
mycursor = mydb.cursor()
mycursor.execute(sql)
mydb.commit()
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 132
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant. Prof. U1.44
MCA-106, Python Programming
sql = "UPDATE customers SET address = 'Canyon 123' WHERE address = 'Valley
345'"
mycursor.execute(sql)
mydb.commit()
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 133
mycursor = mydb.cursor()
mycursor.execute("SELECT * FROM customers LIMIT 5 OFFSET 2")
myresult = mycursor.fetchall()
for x in myresult:
print(x)
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 134
Drop a Table
You can delete an existing table by using the "DROP TABLE" statement:
mycursor = mydb.cursor()
sql = "DROP TABLE customers"
mycursor.execute(sql)
mycursor = mydb.cursor()
sql = "DROP TABLE IF EXISTS customers"
mycursor.execute(sql)
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 135
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant. Prof. U1.45
MCA-106, Python Programming
mycursor = mydb.cursor()
sql = "SELECT \
users.name AS user, \
products.name AS favorite \
FROM users \
INNER JOIN products ON users.fav = products.id"
mycursor.execute(sql)
myresult = mycursor.fetchall()
for x in myresult:
print(x)
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 136
LEFT JOIN
• In the example above, Hannah, and Michael were excluded from the
result, that is because INNER JOIN only shows the records where
there is a match.
• If you want to show all users, even if they do not have a favorite
product, use the LEFT JOIN statement:
Example
• Select all users and their favorite product:
sql = "SELECT \
users.name AS user, \
products.name AS favorite \
FROM users \
LEFT JOIN products ON users.fav = products.id"
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 137
RIGHT JOIN
If you want to return all products, and the users who have them as their
favorite, even if no user have them as their favorite, use the RIGHT JOIN
statement:
Example
• Select all products, and the user(s) who have them as their favorite:
sql = "SELECT \
users.name AS user, \
products.name AS favorite \
FROM users \
RIGHT JOIN products ON users.fav = products.id"
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant Professor – Unit IV 138
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63, by Dr. Saumya, Assistant. Prof. U1.46