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

UNIT -V PACKAGES & GUI

Python packages:

 Simple programs using the built-in functions of packages matplotlib,


 numpy,
 pandas etc.

Illustrative programs:

 create a pandas series using numpy,


 make a pandas dataframe with 2D list.

GUI Programming:

 Tkinter introduction,
 Tkinter and Python Programming,
 Tk Widgets,
 Tkinter examples.
 Python programming with IDE.

Illustrative programs:

 create a GUI marksheet,


 calendar,
 file explorer using Tkinter
What are Python packages?
A python package is a collection of modules. Modules that are related to each other are
mainly put in the same package. When a module from an external package is required in a
program, that package can be imported and its modules can be put to use.

Types of Python Modules

There are two types of python modules:


 Built-in python modules
 User-defined python modules

1. Built-in modules:

Python boasts an extensive collection of built-in modules designed to simplify tasks and
enhance code readability. These modules offer a diverse range of functionality and are readily
accessible without the requirement of installing extra packages. With these built-in modules,
Python provides a comprehensive set of tools and capabilities right out of the box, allowing
developers to accomplish various tasks conveniently and without the hassle of additional
installations.

A list of a few most frequently used built-in python modules is given below

 math: This module is very useful to perform complex mathematical functions such as
trigonometric functions and logarithmic functions.
 date: The date module can be used to work with date and time such as time, date, and
datetime.
 os: Provides a way to interact with the underlying operating system, such as reading or
writing files, executing shell commands, and working with directories.
 sys: Provides access to some variables used or maintained by the Python interpreter, such as
the command-line arguments passed to the script, the Python version, and the location of the
Python executable.
Code
For example, let‟s take the datetime module, which has a submodule called date. When
datetime is imported, it‟ll result in an error, as shown below:

import datetime
date.today()

Output
Traceback (most recent call last):
File "main.py", line 2, in <module>
date.today()
NameError: name 'date' is not defined
The correct way to use the date module is shown below:

from datetime import date


print date.today()

Output
2024-04-15

2. User-defined modules in Python:

User-defined python modules are the modules, which are created by the user to simplify their
project. These modules can contain functions, classes, variables, and other code that you can
reuse across multiple scripts.
Any Python file, whose name is the module‟s name with the .py extension, is a module. A
package is a directory of Python modules that contains an additional __init__.py file, which
distinguishes a package from a directory that is supposed to contain multiple Python scripts.
Packages can be nested to multiple depths if each corresponding directory contains its own
__init__.py file.

When you import a module or a package, the object created by Python is always of type
module.

When you import a package, only the methods and the classes in the __init__.py file of that
package are directly visible.

In Python, the module is a Python script with a .py extension and contains objects such as
classes, functions, etc. Packages in Python extend the concept of the modular approach
further. The package is a folder containing one or more module files; additionally, a special
file "__init__.py" file may be empty but may contain the package list.

Create a Python Package

Let us create a Python package with the name mypackage. Follow the steps given below −
 Create an outer folder to hold the contents of mypackage. Let its name
be packagedemo.
 Inside it, create another folder mypackage. This will be the Python package we are
going to construct. Two Python modules areafunctions.py and mathfunctions.py will
be created inside mypackage.
 Create an empty "__.init__.py" file inside mypackage folder.
 Inside the outer folder, we shall later store a Python script example.py to test our
package.

The file/folder structure should be as shown below −


Using your favorite code editor, save the following two Python
modules in mypackage folder.

Example to Create a Python Package


# mathfunctions.py
def sum(x,y):
val = x+y
return val

def average(x,y):
val = (x+y)/2
return val

def power(x,y):
val = x**y
return val

Create another Python script −


# areafunctions.py
def rectangle(w,h):
area = w*h
return area

def circle(r):
import math
area = math.pi*math.pow(r,2)
return area

Let us now test the myexample package with the help of a Python script above this package
folder. Refer to the folder structure above.
#example.py
from mypackage.areafunctions import rectangle
print ("Area :", rectangle(10,20))

from mypackage.mathsfunctions import average


print ("average:", average(10,20))
This program imports functions from mypackage. If the above script is executed, you should
get following output −
Area : 200
average: 15.0

Define Package List

You can put selected functions or any other resources from the package in the "__init__.py"
file. Let us put the following code in it.
from .areafunctions import circle
from .mathsfunctions import sum, power

To import the available functions from this package, save the following script as
testpackage.py, above the package folder as before.

Example to Define a Package List


#testpackage.py
from mypackage import power, circle

print ("Area of circle:", circle(5))


print ("10 raised to 2:", power(10,2))

Output
Area of circle: 78.53981633974483
10 raised to 2: 100

Simple Plot in Python using Matplotlib


Matplotlib is a Python library that helps in visualizing and analyzing the data and helps in
better understanding of the data with the help of graphical, pictorial visualizations that can be
simulated using the matplotlib library. Matplotlib is a comprehensive library for static,
animated and interactive visualizations.

Installation of matplotlib library


Step 1: Open command manager (just type “cmd” in your windows start search bar)
Step 2: Type the below command in the terminal.
cd Desktop
Step 3: Then type the following command.
pip install matplotlib
Creating a Simple Plot
Python3
# importing the required module
import matplotlib.pyplot as plt

# x axis values
x = [1,2,3]
# corresponding y axis values
y = [2,4,1]

# plotting the points


plt.plot(x, y)

# naming the x axis


plt.xlabel('x - axis')
# naming the y axis
plt.ylabel('y - axis')

# giving a title to my graph


plt.title('My first graph!')

# function to show the plot


plt.show()

Exploring Different Types of Plots, Best Practices, and Tips for Effective Data
Visualization

Matplotlib is a plotting library for the Python programming language and its numerical
mathematics extension NumPy. It provides an object-oriented API for embedding plots into
applications using general-purpose GUI toolkits like Tkinter, wxPython, Qt, or GTK.
Matplotlib is a powerful tool for data visualization in data science and can be used to create a
wide variety of plots, including line plots, scatter plots, bar plots, histograms, 3D plots, and
more. Some of the key features of matplotlib include support for customizable plot styles and
color maps, interactive plot manipulation, and a variety of export options for creating
publication-quality figures.

Line Plot:
A line plot is a way to display data along a number line. It is useful to show trends over time
or to compare multiple sets of data. It is created using the plot function in matplotlib, which
takes in the x and y data as arguments. In the example I gave, the x data is an array of 100
evenly spaced points between 0 and 10 and the y data is the sine of x values.

import matplotlib as plt


x = np.linspace(0, 10, 100)
y = np.sin(x)
plt.plot(x, y)
plt.xlabel('X')
plt.ylabel('sin(X)')
plt.title('Line plot')
plt.show()

Scatter Plot:

A scatter plot is used to show the relationship between two variables. It is created using
the scatter function in matplotlib, which takes in the x and y data as arguments. In the
example I gave, x and y are arrays of random values generated using
the random.normal function from numpy. It shows the correlation or distribution of data
points.

x = np.random.normal(loc=0.0, scale=1.0, size=100)


y = np.random.normal(loc=0.0, scale=1.0, size=100)
plt.scatter(x, y)
plt.xlabel('X')
plt.ylabel('Y')
plt.title('Scatter plot')
plt.show()
Bar Plot:

A bar plot is used to compare the values of different categories. It is created using
the bar function in matplotlib, which takes in the x and y data as arguments. In the example I
gave, x data is an array of categorical values ('A','B','C','D') and y data is an array of values.

x = np.array(['A', 'B', 'C', 'D'])


y = np.array([1, 2, 3, 4])
plt.bar(x, y)
plt.xlabel('Category')
plt.ylabel('Value')
plt.title('Bar plot')
plt.show()
Histogram:

A histogram is used to show the distribution of a single variable. It is created using


the hist function in matplotlib, which takes in the data and the number of bins as arguments.
In the example I gave, the data is an array of 1000 random values generated using
the random.normal function from numpy and number of bins is 30. The histogram plot shows
the frequency of values in different bins, where each bin represents a range of values.

x = np.random.normal(loc=0.0, scale=1.0, size=1000)


plt.hist(x, bins=30)
plt.xlabel('X')
plt.ylabel('Frequency')
plt.title('Histogram')
plt.show()

Box Plot:
A box plot is used to show the distribution and outliers of a set of data. It is created using
the boxplot function in seaborn, which takes in the data and the variables to plot as
arguments. In the example I gave, the data is an array of random values generated using
the random.normal function from numpy.
import seaborn as sns

x = np.random.normal(loc=0.0, scale=1.0, size=100)


sns.boxplot(x=x)
plt.xlabel('X')
plt.title('Box plot')
plt.show()
Heatmap:

A heatmap is used to visualize large data with multiple variables. It is created using
the heatmap function in seaborn, which takes in the data as an argument. In the example I
gave, the data is a 2-D array of random values generated using the random.normal function
from numpy. The color of the cells represents the value of each element in the matrix.

x = np.random.normal(loc=0.0, scale=1.0, size=(10, 10))


sns.heatmap(x)
plt.title('Heatmap')
plt.show()

Violin Plot:
Violin Plots are similar to box plots, but also display the probability density of the data at
different values. They can be created using the violinplot function in seaborn

x = np.random.normal(loc=0.0, scale=1.0, size=100)


sns.violinplot(x)
plt.xlabel('X')
plt.title('Violin plot')
plt.show()

Swarm Plot :

A swarm plot is used to show the distribution of a single categorical variable. It is created
using the swarmplot function in seaborn, which takes in the data and the variables to plot as
arguments. In the example I gave, the x data is an array of random values generated using the
random.normal function from numpy and y data is an array of categorical values(0,1)

x = np.random.normal(loc=0.0, scale=1.0, size=10)


y = np.random.randint(0,2,size=10)
sns.swarmplot(x=x, y=y)
plt.xlabel('X')
plt.ylabel('Category')
plt.title('Swarm plot')
plt.show()
Pie Chart :

A pie chart is used to show the proportion of different categories in a single variable. It is
created using the pie function in matplotlib, which takes in the data and the labels as
arguments. In the example I gave, the data is an array of values representing the size of each
category and the labels are the names of each category. Additionally, you can use the autopct
parameter to add the numerical value of each slice on the chart.

sizes = [15, 30, 45, 10]


labels = ['Frogs', 'Hogs', 'Dogs', 'Logs']
plt.pie(sizes, labels=labels, autopct='%1.1f%%')
plt.axis('equal')
plt.title('Pie chart')
plt.show()
Stacked Bar Plot:

A stacked bar plot is used to show the breakdown of one variable by another. It is created
using the bar function in matplotlib and bottom attribute of bar function. In the example I
gave, Two sets of data are plotted as separate bars, one on top of the other, to show the
breakdown of one variable by another. The legend is used to distinguish between the two sets
of data.

N=5
menMeans = (20, 35, 30, 35, 27)
womenMeans = (25, 32, 34, 20, 25)
menStd = (2, 3, 4, 1, 2)
womenStd = (3, 5, 2, 3, 3)
ind = np.arange(N) # the x locations for the groups
width = 0.35 # the width of the bars: can also be len(x) sequence

p1 = plt.bar(ind, menMeans, width, yerr=menStd)


p2 = plt.bar(ind, womenMeans, width,
bottom=menMeans, yerr=womenStd)

plt.ylabel('Scores')
plt.title('Scores by group and gender')
plt.xticks(ind, ('G1', 'G2', 'G3', 'G4', 'G5'))
plt.yticks(np.arange(0, 81, 10))
plt.legend((p1[0], p2[0]), ('Men', 'Women'))

plt.show()

In conclusion, Matplotlib and Seaborn are powerful libraries for data visualization in data
science. They provide a wide range of options for creating different types of plots, from
simple line plots to more complex heatmaps and violin plots. Each type of plot has its own
strengths and can be used to effectively communicate different types of information.

When creating plots, it's important to consider the context of your data and the audience for
your plots. Choosing the right type of plot depends on the nature of your data and what you
want to communicate with your plot. Additionally, you should also pay attention to the details
of the plot, like labels, scales, and colors, to make sure your plot is easy to read and
understand.

Lastly, always keeping in mind the data you have and what are the important information you
want to show, this will make sure that you choose the right type of plot and customize it to
convey the correct information in a

Python Numpy

Numpy is a general-purpose array-processing package. It provides a high-performance


multidimensional array object, and tools for working with these arrays. It is the fundamental
package for scientific computing with Python. Besides its obvious scientific uses, Numpy can
also be used as an efficient multi-dimensional container of generic data.

Arrays in Numpy
Array in Numpy is a table of elements (usually numbers), all of the same type, indexed by a
tuple of positive integers. In Numpy, number of dimensions of the array is called rank of the
array.A tuple of integers giving the size of the array along each dimension is known as shape of
the array. An array class in Numpy is called as ndarray. Elements in Numpy arrays are
accessed by using square brackets and can be initialized by using nested Python Lists.

Creating a Numpy Array


Arrays in Numpy can be created by multiple ways, with various number of Ranks, defining the
size of the Array. Arrays can also be created with the use of various data types such as lists,
tuples, etc. The type of the resultant array is deduced from the type of the elements in the
sequences.

Note: Type of array can be explicitly defined while creating the array.

Check Number of Dimensions?

NumPy Arrays provides the ndim attribute that returns an integer that tells us how many
dimensions the array have.

Example

Check how many dimensions the arrays have:

import numpy as np
a = np.array(42)
b = np.array([1, 2, 3, 4, 5])
c = np.array([[1, 2, 3], [4, 5, 6]])
d = np.array([[[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]])
print(a.ndim)
print(b.ndim)
print(c.ndim)
print(d.ndim)

Output:
0
1
2
3

# Python program for Creation of Arrays


import numpy as np
# Creating a rank 1 Array
arr = np.array([1, 2, 3])
print("Array with Rank 1: \n",arr)
# Creating a rank 2 Array
arr = np.array([[1, 2, 3],
[4, 5, 6]])
print("Array with Rank 2: \n", arr)
# Creating an array from tuple
arr = np.array((1, 3, 2))
print("\nArray created using "
"passed tuple:\n", arr)
Output:
Array with Rank 1:
[1 2 3]
Array with Rank 2:
[[1 2 3]
[4 5 6]]
Array created using passed tuple:
[1 3 2]

Accessing the array Index


In a numpy array, indexing or accessing the array index can be done in multiple ways. To print
a range of an array, slicing is done. Slicing of an array is defining a range in a new array which
is used to print a range of elements from the original array. Since, sliced array holds a range of
elements of the original array, modifying content with the help of sliced array modifies the
original array content.

# Python program to demonstrate


# indexing in numpy array
import numpy as np
# Initial Array
arr = np.array([[-1, 2, 0, 4],
[4, -0.5, 6, 0],
[2.6, 0, 7, 8],
[3, -7, 4, 2.0]])
print("Initial Array: ")
print(arr)

# Printing a range of Array


# with the use of slicing method
sliced_arr = arr[:2, ::2]
print ("Array with first 2 rows and"
" alternate columns(0 and 2):\n", sliced_arr)

# Printing elements at
# specific Indices
Index_arr = arr[[1, 1, 0, 3],
[3, 2, 1, 0]]
print ("\nElements at indices (1, 3), "
"(1, 2), (0, 1), (3, 0):\n", Index_arr)

Output:
Initial Array:
[[-1. 2. 0. 4. ]
[ 4. -0.5 6. 0. ]
[ 2.6 0. 7. 8. ]
[ 3. -7. 4. 2. ]]
Array with first 2 rows and alternate columns(0 and 2):
[[-1. 0.]
[ 4. 6.]]
Elements at indices (1, 3), (1, 2), (0, 1), (3, 0):
[0. 6. 2. 3.]

Basic Array Operations


In numpy, arrays allow a wide range of operations which can be performed on a particular array
or a combination of Arrays. These operation include some basic Mathematical operation as well
as Unary and Binary operations.

# Python program to demonstrate


# basic operations on single array
import numpy as np
# Defining Array 1
a = np.array([[1, 2], [3, 4]])
# Defining Array 2
b = np.array([[4, 3], [2, 1]])
# Adding 1 to every element
print ("Adding 1 to every element:", a + 1)
# Subtracting 2 from each element
print ("\nSubtracting 2 from each element:", b - 2)
# sum of array elements
# Performing Unary operations
print ("\nSum of all array "
"elements: ", a.sum())
# Adding two arrays
# Performing Binary operations
print ("\nArray sum:\n", a + b)

Output:
Adding 1 to every element:
[[2 3]
[4 5]]
Subtracting 2 from each element:
[[ 2 1]
[ 0 -1]]
Sum of all array elements: 10
Array sum:
[[5 5]
[5 5]]

Data Types in Numpy


Every Numpy array is a table of elements (usually numbers), all of the same type, indexed by a
tuple of positive integers. Every ndarray has an associated data type (dtype) object. This data
type object (dtype) provides information about the layout of the array. The values of an ndarray
are stored in a buffer which can be thought of as a contiguous block of memory bytes which can
be interpreted by the dtype object. Numpy provides a large set of numeric datatypes that can be
used to construct arrays. At the time of Array creation, Numpy tries to guess a datatype, but
functions that construct arrays usually also include an optional argument to explicitly specify
the datatype.
Constructing a Datatype Object
In Numpy, datatypes of Arrays need not to be defined unless a specific datatype is required.
Numpy tries to guess the datatype for Arrays which are not predefined in the constructor
function.
# Python Program to create
# a data type object
import numpy as np
# Integer datatype
# guessed by Numpy
x = np.array([1, 2])
print("Integer Datatype: ")
print(x.dtype)
# Float datatype
# guessed by Numpy
x = np.array([1.0, 2.0])
print("\nFloat Datatype: ")
print(x.dtype)
# Forced Datatype
x = np.array([1, 2], dtype = np.int64)
print("\nForcing a Datatype: ")
print(x.dtype)

Output:
Integer Datatype:
int64
Float Datatype:
float64
Forcing a Datatype:
int64

Math Operations on DataType array


In Numpy arrays, basic mathematical operations are performed element-wise on the array.
These operations are applied both as operator overloads and as functions. Many useful
functions are provided in Numpy for performing computations on Arrays such as sum: for
addition of Array elements, T: for Transpose of elements, etc.

# Python Program to create


# a data type object
import numpy as np
# First Array
arr1 = np.array([[4, 7], [2, 6]],
dtype = np.float64)
# Second Array
arr2 = np.array([[3, 6], [2, 8]],
dtype = np.float64)
# Addition of two Arrays
Sum = np.add(arr1, arr2)
print("Addition of Two Arrays: ")
print(Sum)
# Addition of all Array elements
# using predefined sum method
Sum1 = np.sum(arr1)
print("\nAddition of Array elements: ")
print(Sum1)

# Square root of Array


Sqrt = np.sqrt(arr1)
print("\nSquare root of Array1 elements: ")
print(Sqrt)
# Transpose of Array
# using In-built function 'T'
Trans_arr = arr1.T
print("\nTranspose of Array: ")
print(Trans_arr)

Output:
Addition of Two Arrays:
[[ 7. 13.]
[ 4. 14.]]
Addition of Array elements:
19.0
Square root of Array1 elements:
[[2. 2.64575131]
[1.41421356 2.44948974]]
Transpose of Array:
[[4. 2.]
[7. 6.]]

Important Numpy Functions in Python


Here are some of the important NumPy functions in Python which every Data scientist must
know:
1. np.array(): This function is used to create an array in NumPy.
Example Code:
import numpy as np
arr = np.array([1, 2, 3])
print(arr)
Output:
[1 2 3]
Explanation: In this example, we created a numpy array by passing a list of 3 numbers as a
parameter into np.array() function.

2. np.arange(): This function is used to create an array with a range of values.


Example Code:
import numpy as np
arr = np.arange(1, 6)
print(arr)
Output:
[1 2 3 4 5]
Explanation: In this example, we created a numpy array with a range of values which is
(1,6), where 6 is excluded.

3. np.zeros(): This function is used to create an array filled with zeros.


Example Code:
import numpy as np
arr = np.zeros(3)
print(arr)
Output:
[0. 0. 0.]
Explanation: In this example, we created an array of size 3 which is filled with only zeroes.

4. np.ones(): This function is used to create an array filled with ones.


Example Code:
import numpy as np

arr = np.ones(3)
print(arr)
Output:
[1. 1. 1.]
Explanation: In this example, we created an array of size 3 which is filled with only ones.

5. np.linspace(): This function is used to create an array with a specified number of evenly
spaced values.
Example Code:
import numpy as np
arr = np.linspace(0, 1, 5)
print(arr)
Output:
[0. 0.25 0.5 0.75 1. ]

6. np.random.rand(): This function is used to create an array with random values between 0
and 1.
Example Code:
import numpy as np

arr = np.random.rand(3)
print(arr)
Output:
[0.5488135 0.71518937 0.60276338]
Explanation: In this example, we created an array of size 3 which is filled with random
values which lie between 0 and 1.

7. np.random.randint(): This function is used to create an array with random integer values
between a specified range.
Example Code:
import numpy as np

arr = np.random.randint(0, 10, 5)


print(arr)
Output:
[1 5 8 9 8]
Explanation: In this example, we created an array of size 5 which is filled with random
values which lie between 0 and 10.

8. np.max(): This function is used to find the maximum value in an array.


Example Code:
import numpy as np
arr = np.array([1, 2, 3])
max_value = np.max(arr)
print(max_value)
Output:
3
Explanation: In this example, we used max() function to find the max element in our array,
which is 3 in this case.

9. np.min(): This function is used to find the minimum value in an array.


Example Code:
import numpy as np

arr = np.array([1, 2, 3])


min_value = np.min(arr)
print(min_value)
Output:
1
Explanation: In this example, we used min() function to find the min element in our array,
which is 1 in this case.

10. np.mean(): This function is used to find the mean value of an array.
Example Code:
import numpy as np
arr = np.array([1, 2, 3])
mean_value = np.mean(arr)
print(mean_value)
Output:
2.0

11. np.median(): This function is used to find the median value of an array.
Example Code:
import numpy as np

arr = np.array([1, 2, 3])


median_value = np.median(arr)
print(median_value)
Output:
2.0

12. np.dot(): This function is used to find the dot product of two arrays.
Example Code:
import numpy as np
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
dot_product = np.dot(arr1, arr2)
print(dot_product)
Output:
32
Explanation: In this example, we created two NumPy arrays called array1 and array2, which
contain the values [1, 2, 3] and [4, 5, 6], respectively. We then used np.dot() to calculate the
dot product of these two arrays, which is equal to 14 + 25 + 3*6, or 32. The resulting value is
stored in the variable result and printed to the console.

Python Pandas
The term "Pandas" refers to an open-source library for manipulating high-performance data in
Python. This instructional exercise is intended for the two novices and experts.
It was created in 2008 by Wes McKinney and is used for data analysis in Python. Pandas is an
open-source library that provides high-performance data manipulation in Python. All of the
basic and advanced concepts of Pandas, such as Numpy, data operation, and time series, are
covered in our tutorial.

Pandas Introduction

The name of Pandas is gotten from the word Board Information, and that implies an
Econometrics from Multi-faceted information. It was created in 2008 by Wes McKinney and
is used for data analysis in Python.

Processing, such as restructuring, cleaning, merging, etc., is necessary for data analysis.
Numpy, Scipy, Cython, and Panda are just a few of the fast data processing tools available.
Yet, we incline toward Pandas since working with Pandas is quick, basic and more expressive
than different apparatuses.

Since Pandas is built on top of the Numpy bundle, it is expected that Numpy will work with
Pandas.

Before Pandas, Python was able for information planning, however it just offered restricted
help for information investigation. As a result, Pandas entered the picture and enhanced data
analysis capabilities. Regardless of the source of the data, it can carry out the five crucial
steps that are necessary for processing and analyzing it: load, manipulate, prepare, model, and
analyze.

Key Features of Pandas

 It has a DataFrame object that is quick and effective, with both standard and
custom indexing.
 Utilized for reshaping and turning of the informational indexes.
 For aggregations and transformations, group by data.
 It is used to align the data and integrate the data that is missing.
 Provide Time Series functionality.
 Process a variety of data sets in various formats, such as matrix data,
heterogeneous tabular data, and time series.
 Manage the data sets' multiple operations, including subsetting, slicing, filtering,
groupBy, reordering, and reshaping.
 It incorporates with different libraries like SciPy, and scikit-learn.
 Performs quickly, and the Cython can be used to accelerate it even further.

Benefits of Pandas

The following are the advantages of pandas overusing other languages:


Representation of Data: Through its DataFrame and Series, it presents the data in a manner
that is appropriate for data analysis.

Clear code: Pandas' clear API lets you concentrate on the most important part of the code. In
this way, it gives clear and brief code to the client.

DataFrame and Series are the two data structures that Pandas provides for processing data.
These data structures are discussed below:

1) Series

A one-dimensional array capable of storing a variety of data types is how it is defined. The
term "index" refers to the row labels of a series. We can without much of a stretch believer
the rundown, tuple, and word reference into series utilizing "series' technique. Multiple
columns cannot be included in a Series. Only one parameter exists:

Data: It can be any list, dictionary, or scalar value.

Creating Series from Array:

Before creating a Series, Firstly, we have to import the numpy module and then use array()
function in the program.

import pandas as pd
import numpy as np
info = np.array(['P','a','n','d','a','s'])
a = pd.Series(info)
print(a)

Output

0 P
1 a
2 n
3 d
4 a
5 s
dtype: object

Explanation: In this code, firstly, we have imported the pandas and numpy library with
the pd and np alias. Then, we have taken a variable named "info" that consist of an array of
some values. We have called the info variable through a Series method and defined it in an
"a" variable. The Series has printed by calling the print(a) method.

Python Pandas DataFrame

It is a generally utilized information design of pandas and works with a two-layered exhibit
with named tomahawks (lines and segments). As a standard method for storing data,
DataFrame has two distinct indexes-row index and column index. It has the following
characteristics:

The sections can be heterogeneous sorts like int, bool, etc.

It can be thought of as a series structure dictionary with indexed rows and columns. It is
referred to as "columns" for rows and "index" for columns.

Create a DataFrame using List:

We can easily create a DataFrame in Pandas using list.

import pandas as pd
# a list of strings
x = ['Python', 'Pandas']
# Calling DataFrame constructor on list
df = pd.DataFrame(x)
print(df)

Output

0
0 Python
1 Pandas

Explanation: In this code, we have characterized a variable named "x" that comprise of
string values. On a list, the values are being printed by calling the DataFrame constructor.
Python Pandas Series

The Pandas Series can be defined as a one-dimensional array that is capable of storing various data
types. We can easily convert the list, tuple, and dictionary into series using "series' method. The
row labels of series are called the index. A Series cannot contain multiple columns. It has the
following parameter:

o data: It can be any list, dictionary, or scalar value.


o index: The value of the index should be unique and hashable. It must be of the same length
as data. If we do not pass any index, default np.arrange(n) will be used.
o dtype: It refers to the data type of series.
o copy: It is used for copying the data.

Creating a Series:

We can create a Series in two ways:

1. Create an empty Series


2. Create a Series using inputs.

Create an Empty Series:

We can easily create an empty series in Pandas which means it will not have any value.

The syntax that is used for creating an Empty Series:

1. <series object> = pandas.Series()

The below example creates an Empty Series type object that has no values and having default
datatype, i.e., float64.

Example

1. import pandas as pd
2. x = pd.Series()
3. print (x)

Output

Series([], dtype: float64)

Creating a Series using inputs:


We can create Series by using various inputs:

o Array
o Dict
o Scalar value

Creating Series from Array:

Before creating a Series, firstly, we have to import the numpy module and then use array() function
in the program. If the data is ndarray, then the passed index must be of the same length.

If we do not pass an index, then by default index of range(n) is being passed where n defines the
length of an array, i.e., [0,1,2,....range(len(array))-1].

Example

1. import pandas as pd
2. import numpy as np
3. info = np.array(['P','a','n','d','a','s'])
4. a = pd.Series(info)
5. print(a)

Output

0 P
1 a
2 n
3 d
4 a
5 s
dtype: object

Create a Series from dict

We can also create a Series from dict. If the dictionary object is being passed as an input and
the index is not specified, then the dictionary keys are taken in a sorted order to construct the
index.

If index is passed, then values correspond to a particular label in the index will be extracted from
the dictionary.

1. #import the pandas library


2. import pandas as pd
3. import numpy as np
4. info = {'x' : 0., 'y' : 1., 'z' : 2.}
5. a = pd.Series(info)
6. print (a)

Output

x 0.0
y 1.0
z 2.0
dtype: float64

Create a Series using Scalar:

If we take the scalar values, then the index must be provided. The scalar value will be repeated for
matching the length of the index.

1. #import pandas library


2. import pandas as pd
3. import numpy as np
4. x = pd.Series(4, index=[0, 1, 2, 3])
5. print (x)

Output

0 4
1 4
2 4
3 4
dtype: int64

Accessing data from series with Position:

Once you create the Series type object, you can access its indexes, data, and even individual
elements.

The data in the Series can be accessed similar to that in the ndarray.

1. import pandas as pd
2. x = pd.Series([1,2,3],index = ['a','b','c'])
3. #retrieve the first element
4. print (x[0])

Output

1
Series object attributes

The Series attribute is defined as any information related to the Series object such as size, datatype.
etc. Below are some of the attributes that you can use to get the information about the Series object:

Attributes Description
Series.index Defines the index of the Series.
Series.shape It returns a tuple of shape of the data.
Series.dtype It returns the data type of the data.
Series.size It returns the size of the data.
Series.empty It returns True if Series object is empty, otherwise returns false.
Series.hasnans It returns True if there are any NaN values, otherwise returns
false.
Series.nbytes It returns the number of bytes in the data.
Series.ndim It returns the number of dimensions in the data.
Series.itemsize It returns the size of the datatype of item.

Retrieving Index array and data array of a series object

We can retrieve the index array and data array of an existing Series object by using the attributes
index and values.

1. import numpy as np
2. import pandas as pd
3. x=pd.Series(data=[2,4,6,8])
4. y=pd.Series(data=[11.2,18.6,22.5], index=['a','b','c'])
5. print(x.index)
6. print(x.values)
7. print(y.index)
8. print(y.values)

Output

RangeIndex(start=0, stop=4, step=1)


[2 4 6 8]
Index(['a', 'b', 'c'], dtype='object')
[11.2 18.6 22.5]

Retrieving Types (dtype) and Size of Type (itemsize)

You can use attribute dtype with Series object as <objectname> dtype for retrieving the data type of
an individual element of a series object, you can use the itemsize attribute to show the number of
bytes allocated to each data item.

1. import numpy as np
2. import pandas as pd
3. a=pd.Series(data=[1,2,3,4])
4. b=pd.Series(data=[4.9,8.2,5.6],
5. index=['x','y','z'])
6. print(a.dtype)
7. print(a.itemsize)
8. print(b.dtype)
9. print(b.itemsize)

Output

int64
8
float64
8

Retrieving Shape

The shape of the Series object defines total number of elements including missing or empty
values(NaN).

1. import numpy as np
2. import pandas as pd
3. a=pd.Series(data=[1,2,3,4])
4. b=pd.Series(data=[4.9,8.2,5.6],index=['x','y','z'])
5. print(a.shape)
6. print(b.shape)

Output

(4,)
(3,)

Retrieving Dimension, Size and Number of bytes:

1. import numpy as np
2. import pandas as pd
3. a=pd.Series(data=[1,2,3,4])
4. b=pd.Series(data=[4.9,8.2,5.6],
5. index=['x','y','z'])
6. print(a.ndim, b.ndim)
7. print(a.size, b.size)
8. print(a.nbytes, b.nbytes)

Output

11
43
32 24

Checking Emptiness and Presence of NaNs

To check the Series object is empty, you can use the empty attribute. Similarly, to check if a series
object contains some NaN values or not, you can use the hasans attribute.

Example

1. import numpy as np
2. import pandas as pd
3. a=pd.Series(data=[1,2,3,np.NaN])
4. b=pd.Series(data=[4.9,8.2,5.6],index=['x','y','z'])
5. c=pd.Series()
6. print(a.empty,b.empty,c.empty)
7. print(a.hasnans,b.hasnans,c.hasnans)
8. print(len(a),len(b))
9. print(a.count( ),b.count( ))

Output

False False True


True False False
4 3
3 3

Series Functions

There are some functions used in Series which are as follows:

Functions Description
Pandas Series.map() Map the values from two series that have a common column.
Pandas Series.std() Calculate the standard deviation of the given set of numbers,
DataFrame, column, and rows.
Pandas Series.to_frame() Convert the series object to the dataframe.
Pandas Returns a Series that contain counts of unique values.
Series.value_counts()
Illustrative programs:

 create a pandas series using numpy,


 make a pandas dataframe with 2D list.

Write a Pandas program to convert a NumPy array to a Pandas series.


Sample NumPy array: d1 = [10, 20, 30, 40, 50]

Python Code :
import numpy as np
import pandas as pd
np_array = np.array([10, 20, 30, 40, 50])
print("NumPy array:")
print(np_array)
new_series = pd.Series(np_array)
print("Converted Pandas series:")
print(new_series)

Output:
NumPy array:
[10 20 30 40 50]
Converted Pandas series:
0 10
1 20
2 30
3 40
4 50
dtype: int64

Explanation:
np.array([10, 20, 30, 40, 50]): This code creates a NumPy array 'np_array' containing a
sequence of five integers: [10, 20, 30, 40, 50]. new_series = pd.Series(np_array): This line
creates a new Pandas Series object 'new_series' from the NumPy array using the pd.Series()
constructor. The resulting Series object will have the same values as the NumPy array, and
the default index will be assigned to each element starting from 0 and increasing by 1 for each
subsequent element

Make a Pandas DataFrame with two-dimensional list | Python

we will illustrate the process of creating a Pandas DataFrame with the two-dimensional
list. Python is widely recognized for its effectiveness in data analysis, thanks to its robust
ecosystem of data-centric packages. Among these packages, Pandas stands out, streamlining
the import and analysis of data. There are various methods to achieve a Pandas DataFrame,
and in this article, we will focus on creating one using a two-dimensional list.

Pandas DataFrame with Two-dimensional List


There are several methods for creating a Pandas DataFrame with the two-dimensional list. In
this context, we will explain some commonly used approaches.
Using pd.DataFrame()
Using pd.DataFrame.from_records()
Using pd.DataFrame.from_dict()
Using Specifying Data Types

Create Pandas Dataframe from 2D List using pd.DataFrame()


In this example below code creates a Pandas DataFrame („df‟) from a two-dimensional list
(„lst‟) with specified column names („Tag‟ and „number‟) and prints the resulting DataFrame.

# import pandas as pd
import pandas as pd
# List1
lst = [['Geek', 25], ['is', 30],
['for', 26], ['Geeksforgeeks', 22]]
# creating df object with columns specified
df = pd.DataFrame(lst, columns =['Tag', 'number'])
print(df )

Output :
Tag number
0 Geek 25
1 is 30
2 for 26
3 Geeksforgeeks 22

Create Pandas Dataframe from 2D List using pd.DataFrame.from_records()


In this example below code uses the pandas library in Python to create a DataFrame from a
two-dimensional list (data). The DataFrame has columns with names „Name‟, „Age‟, and
„Occupation‟. The print(df) statement will display the DataFrame. Here‟s the expected output:
Python3

import pandas as pd

# Two-dimensional list
data = [['Geek1', 28, 'Analyst'],
['Geek2', 35, 'Manager'],
['Geek3', 29, 'Developer']]
# Column names
columns = ['Name', 'Age', 'Occupation']

# Creating DataFrame using pd.DataFrame.from_records()


df = pd.DataFrame.from_records(data, columns=columns)

# Displaying the DataFrame


print(df)

Output:
Name Age Occupation
0 Geek1 28 Analyst
1 Geek2 35 Manager
2 Geek3 29 Developer

Create Pandas Dataframe from 2D List using pd.DataFrame.from_dict()


In this example below code uses the pandas library in Python to create a DataFrame from a
two-dimensional list (data). Instead of using pd.DataFrame.from_records(), this time it
uses pd.DataFrame.from_dict() along with the zip function to transpose the data.
Python3

import pandas as pd

# Two-dimensional list
data = [['Geek1', 26, 'Scientist'],
['Geek2', 31, 'Researcher'],
['Geek3', 24, 'Engineer']]

# Column names
columns = ['Name', 'Age', 'Occupation']

# Creating DataFrame using pd.DataFrame.from_dict()


df = pd.DataFrame.from_dict(dict(zip(columns, zip(*data))))

# Displaying the DataFrame


print(df)

Output:
Name Age Occupation
0 Geek1 26 Scientist
1 Geek2 31 Researcher
2 Geek3 24 Engineer

Create Pandas Dataframe from 2D List using Specifying Data Types


In this example below code uses the pandas library in Python to create a DataFrame from a
two-dimensional list (data). The DataFrame has columns with names „FName‟, „LName‟, and
„Age‟. The specified data types for all columns are set to float using the dtype parameter.

import pandas as pd
# Two-dimensional list
data = [['Geek1', 'Reacher', 25],
['Geek2', 'Pete', 30],
['Geek3', 'Wilson', 26],
['Geek4', 'Williams', 22]]
# Column names
columns = ['FName', 'LName', 'Age']
# Creating DataFrame with specified data types
df = pd.DataFrame(data, columns=columns, dtype=float)
# Displaying the DataFrame
print(df)

Output :
FName LName Age
0 Geek1 Reacher 25.0
1 Geek2 Pete 30.0
2 Geek3 Wilson 26.0
3 Geek4 Williams 22.0

Don't miss your chance to ride the wave of the data revolution! Every industry is scaling new
heights by tapping into the power of data. Sharpen your skills and become a part of the hottest
trend in the 21st century.

Python Tkinter
Python provides the standard library Tkinter for creating the graphical user interface for
desktop based applications.

Developing desktop based applications with python Tkinter is not a complex task. An empty
Tkinter top-level window can be created by using the following steps.

1. import the Tkinter module.


2. Create the main application window.
3. Add the widgets like labels, buttons, frames, etc. to the window.
4. Call the main event loop so that the actions can take place on the user's computer
screen.

Example

1. # !/usr/bin/python3
2. from tkinter import *
3. #creating the application main window.
4. top = Tk()
5. #Entering the event main loop
6. top.mainloop()
Output:

Tkinter widgets

There are various widgets like button, canvas, checkbutton, entry, etc. that are used to build
the python GUI applications.

SN Widget Description
1 Button The Button is used to add various kinds of buttons to the python
application.
2 Canvas The canvas widget is used to draw the canvas on the window.
3 Checkbutton The Checkbutton is used to display the CheckButton on the window.
4 Entry The entry widget is used to display the single-line text field to the user.
It is commonly used to accept user values.
5 Frame It can be defined as a container to which, another widget can be added
and organized.
6 Label A label is a text used to display some message or information about the
other widgets.
7 ListBox The ListBox widget is used to display a list of options to the user.
8 Menubutton The Menubutton is used to display the menu items to the user.
9 Menu It is used to add menu items to the user.
10 Message The Message widget is used to display the message-box to the user.
11 Radiobutton The Radiobutton is different from a checkbutton. Here, the user is
provided with various options and the user can select only one option
among them.
12 Scale It is used to provide the slider to the user.
13 Scrollbar It provides the scrollbar to the user so that the user can scroll the
window up and down.
14 Text It is different from Entry because it provides a multi-line text field to
the user so that the user can write the text and edit the text inside it.
14 Toplevel It is used to create a separate window container.
15 Spinbox It is an entry widget used to select from options of values.
16 PanedWindow It is like a container widget that contains horizontal or vertical panes.
17 LabelFrame A LabelFrame is a container widget that acts as the container
18 MessageBox This module is used to display the message-box in the desktop based
applications.

Python Tkinter Geometry

The Tkinter geometry specifies the method by using which, the widgets are represented on
display. The python Tkinter provides the following geometry methods.

1. The pack() method


2. The grid() method
3. The place() method

Let's discuss each one of them in detail.

Python Tkinter pack() method

The pack() widget is used to organize widget in the block. The positions widgets added to the
python application using the pack() method can be controlled by using the various options
specified in the method call.

However, the controls are less and widgets are generally added in the less organized manner.

The syntax to use the pack() is given below.

syntax

1. widget.pack(options)
A list of possible options that can be passed in pack() is given below.

o expand: If the expand is set to true, the widget expands to fill any space.
o Fill: By default, the fill is set to NONE. However, we can set it to X or Y to determine
whether the widget contains any extra space.
o size: it represents the side of the parent to which the widget is to be placed on the
window.

Example
1. # !/usr/bin/python3
2. from tkinter import *
3. parent = Tk()
4. redbutton = Button(parent, text = "Red", fg = "red")
5. redbutton.pack( side = LEFT)
6. greenbutton = Button(parent, text = "Black", fg = "black")
7. greenbutton.pack( side = RIGHT )
8. bluebutton = Button(parent, text = "Blue", fg = "blue")
9. bluebutton.pack( side = TOP )
10. blackbutton = Button(parent, text = "Green", fg = "red")
11. blackbutton.pack( side = BOTTOM)
12. parent.mainloop()
Output:

Python Tkinter grid() method

The grid() geometry manager organizes the widgets in the tabular form. We can specify the
rows and columns as the options in the method call. We can also specify the column span
(width) or rowspan(height) of a widget.

This is a more organized way to place the widgets to the python application. The syntax to
use the grid() is given below.

Syntax

1. widget.grid(options)
A list of possible options that can be passed inside the grid() method is given below.

o Column
The column number in which the widget is to be placed. The leftmost column is
represented by 0.
o Columnspan
The width of the widget. It represents the number of columns up to which, the column
is expanded.
o ipadx, ipady
o It represents the number of pixels to pad the widget inside the widget's border.
o padx, pady
o It represents the number of pixels to pad the widget outside the widget's border.
o row
The row number in which the widget is to be placed. The topmost row is represented
by 0.
o rowspan
The height of the widget, i.e. the number of the row up to which the widget is
expanded.
o Sticky
If the cell is larger than a widget, then sticky is used to specify the position of the
widget inside the cell. It may be the concatenation of the sticky letters representing the
position of the widget. It may be N, E, W, S, NE, NW, NS, EW, ES.

Example

# !/usr/bin/python3
from tkinter import *
parent = Tk()
name = Label(parent,text = "Name").grid(row = 0, column = 0)
e1 = Entry(parent).grid(row = 0, column = 1)
password = Label(parent,text = "Password").grid(row = 1, column = 0)
e2 = Entry(parent).grid(row = 1, column = 1)
submit = Button(parent, text = "Submit").grid(row = 4, column = 0)
parent.mainloop()
Output:

Python Tkinter place() method

The place() geometry manager organizes the widgets to the specific x and y coordinates.

Syntax

1. widget.place(options)
A list of possible options is given below.

o Anchor: It represents the exact position of the widget within the container. The
default value (direction) is NW (the upper left corner)
o bordermode: The default value of the border type is INSIDE that refers to ignore the
parent's inside the border. The other option is OUTSIDE.
o height, width: It refers to the height and width in pixels.
o relheight, relwidth: It is represented as the float between 0.0 and 1.0 indicating the
fraction of the parent's height and width.
o relx, rely: It is represented as the float between 0.0 and 1.0 that is the offset in the
horizontal and vertical direction.
o x, y: It refers to the horizontal and vertical offset in the pixels.

Example

# !/usr/bin/python3
from tkinter import *
top = Tk()
top.geometry("400x250")
name = Label(top, text = "Name").place(x = 30,y = 50)
email = Label(top, text = "Email").place(x = 30, y = 90)
password = Label(top, text = "Password").place(x = 30, y = 130)
e1 = Entry(top).place(x = 80, y = 50)
e2 = Entry(top).place(x = 80, y = 90)
e3 = Entry(top).place(x = 95, y = 130)
top.mainloop()
Output:
Create a Date Picker Calendar in Tkinter

Tkinter is a popular Python library for creating and developing applications. It has various
methods and functions that can be used for adding multiple features in an application.

Tkcalendar is one of the tkinter packages that can be used to create GUI-based calendars in
the window and thus, we can perform multiple operations like selecting the data, picking and
scheduling the event through the calendar application and many more.

However, in this article, we will see how we can create a Date Picker calendar using the
Tkcalendar package. Before that, we have to install the package in our local environment
using pip install tkcalendar.

Once installed, we will create an instance of tkcalendar and create a button to get the date.

#Import the libraries


from tkinter import *
from tkcalendar import *
#Create an instance of tkinter frame or window
win= Tk()
win.title("Calendar")
win.geometry("700x600")
cal= Calendar(win, selectmode="day",year= 2021, month=3, day=3)
cal.pack(pady=20)
#Define Function to select the date
def get_date():
label.config(text=cal.get_date())
#Create a button to pick the date from the calendar
button= Button(win, text= "Select the Date", command= get_date)
button.pack(pady=20)
#Create Label for displaying selected Date
label= Label(win, text="")
label.pack(pady=20)
win.mainloop()

You might also like