SBLC 1
SBLC 1
Date of Performance :
Date of Submission :
AIM: Program to demonstrate Data Frames using Pandas
THEORY:
A Data frame is a two-dimensional data structure, i.e., data is aligned in a tabular fashion in rows and
columns.
Features of DataFrame
● Potentially columns are of different types
● Size – Mutable
● Labeled axes (rows and columns)
● Can Perform Arithmetic operations on rows and columns
Structure
Let us assume that we are creating a data frame with student‟s data.
import pandas as pd
data =[['Alex',10],['Bob',12],['Clarke',13]]
df = pd.DataFrame(data,columns=['Name','Age'])
print df
Its output is as follows −
Name Age
0 Alex 10
1 Bob 12
2 Clarke 13
Example 3
import pandas as pd
data =[['Alex',10],['Bob',12],['Clarke',13]]
df = pd.DataFrame(data,columns=['Name','Age'],dtype=float)
print df
Its output is as follows −
Name Age
0 Alex 10.0
1 Bob 12.0
2 Clarke 13.0
Note − Observe, the dtype parameter changes the type of Age column to floating point.
Create a DataFrame from Dict of ndarrays / Lists
All the ndarrays must be of same length. If index is passed, then the length of the index should equal to
the length of the arrays.
If no index is passed, then by default, index will be range(n), where n is the array length.
Example 1
import pandas as pd
data ={'Name':['Tom','Jack','Steve','Ricky'],'Age':[28,34,29,42]}
df = pd.DataFrame(data)
print df
Its output is as follows −
Age Name
0 28 Tom
1 34 Jack
2 29 Steve
3 42 Ricky
Note − Observe the values 0,1,2,3. They are the default index assigned to each using the function
range(n).
Example 2
Let us now create an indexed DataFrame using arrays.
import pandas as pd
data ={'Name':['Tom','Jack','Steve','Ricky'],'Age':[28,34,29,42]}
df = pd.DataFrame(data, index=['rank1','rank2','rank3','rank4'])
print df
Its output is as follows −
Age Name
rank1 28 Tom
rank2 34 Jack
rank3 29 Steve
rank4 42 Ricky
Note − Observe, the index parameter assigns an index to each row.
Create a DataFrame from List of Dicts
List of Dictionaries can be passed as input data to create a DataFrame. The dictionary keys are by
default taken as column names.
Example 1
The following example shows how to create a DataFrame by passing a list of dictionaries.
import pandas as pd
data =[{'a':1,'b':2},{'a':5,'b':10,'c':20}]
df = pd.DataFrame(data)
print df
Its output is as follows −
a b c
0 1 2 NaN
1 5 10 20.0
Note − Observe, NaN (Not a Number) is appended in missing areas.
Example 2
The following example shows how to create a DataFrame by passing a list of dictionaries and the row
indices.
import pandas as pd
data =[{'a':1,'b':2},{'a':5,'b':10,'c':20}]
df = pd.DataFrame(data, index=['first','second'])
print df
Its output is as follows −
a b c
first 1 2 NaN
second 5 10 20.0
Example 3
The following example shows how to create a DataFrame with a list of dictionaries, row indices, and
column indices.
import pandas as pd
data =[{'a':1,'b':2},{'a':5,'b':10,'c':20}]
#With two column indices with one index with other name
df2 = pd.DataFrame(data, index=['first','second'], columns=['a','b1'])
print df1
print df2
Its output is as follows −
#df1 output
a b
first 1 2
second 5 10
#df2
output b1
a
first 1 NaN
second 5 NaN
Note − Observe, df2 DataFrame is created with a column index other than the dictionary key; thus,
appended the NaN‟s in place. Whereas, df1 is created with column indices same as dictionary keys, so
NaN‟s appended.
Create a DataFrame from Dict of Series
Dictionary of Series can be passed to form a DataFrame. The resultant index is the union of all the series
indexes passed.
Example
import pandas as pd
df = pd.DataFrame(d)
print df
Its output is as follows −
one two
a 1.0 1
b 2.0 2
c 3.0 3
d NaN 4
Note − Observe, for the series one, there is no label ‘d’ passed, but in the result, for the d label, NaN is appended
with NaN.
Let us now understand column selection, addition, and deletion through examples.
Column Selection
We will understand this by selecting a column from the DataFrame.
Example
import pandas as pd
df = pd.DataFrame(d)
print df ['one']
Its output is as follows −
a 1.0
b 2.0
c 3.0
d NaN
Name: one, dtype: float64
Column Addition
We will understand this by adding a new column to an existing data frame.
Example
import pandas as pd
df = pd.DataFrame(d)
# Adding a new column to an existing DataFrame object with column label by passing
new series
print df
Its output is as follows −
Adding a new column by passing as Series:
one two three
a 1.0 1 10.0
b 2.0 2 20.0
c 3.0 3 30.0
d NaN 4 NaN
df = pd.DataFrame(d)
print("Our dataframe is:")
print df
import pandas as pd
df = pd.DataFrame(d)
print df.loc['b']
Its output is as follows −
one 2.0
two 2.0
Name: b, dtype: float64
The result is a series with labels as column names of the DataFrame. And, the Name of the series is the
label with which it is retrieved.
Selection by integer location
Rows can be selected by passing integer location to an iloc function.
import pandas as pd
df = pd.DataFrame(d)
print df.iloc[2]
Its output is as follows −
one 3.0
two 3.0
Name: c, dtype: float64
Slice Rows
Multiple rows can be selected using „ : ‟ operator.
import pandas as pd
d ={'one': pd.Series([1,2,3], index=['a','b','c']),
'two': pd.Series([1,2,3,4], index=['a','b','c','d'])}
df = pd.DataFrame(d)
print df[2:4]
Its output is as follows −
one two
c 3.0 3
d NaN 4
Addition of Rows
Add new rows to a DataFrame using the append function. This function will append the rows at the end.
import pandas as pd
df = df.append(df2)
print df
Its output is as follows −
a b
0 1 2
1 3 4
0 5 6
1 7 8
Deletion of Rows
Use index label to delete or drop rows from a DataFrame. If label is duplicated, then multiple rows will
be dropped.
If you observe, in the above example, the labels are duplicate. Let us drop a label and will see how many
rows will get dropped.
import pandas as pd
df = pd.DataFrame([[1,2],[3,4]], columns =['a','b'])
df2 = pd.DataFrame([[5,6],[7,8]], columns =['a','b'])
df = df.append(df2)
print df
Its output is as follows −
a b
1 3 4
1 7 8
CODE
import pandas as pd
# Create a DataFrame
data = {'Name': ['Alice', 'Bob', 'Charlie', 'David', 'Eve'],
'Age': [25, 30, 35, 40, 45],
'City': ['New York', 'Los Angeles', 'Chicago', 'Houston', 'Phoenix']}
df = pd.DataFrame(data)
Date of Submission :
AIM: Menu driven program for data structure using built in function for link list, stack
and queues.
THEORY:
A list is a data structure in Python that is a mutable, or changeable, ordered sequence of elements.
Each element or value that is inside of a list is called an item. Just as strings are defined as
characters between quotes, lists are defined by having values between square brackets [ ]. Lists
are great to use when you want to work with many related values. They enable you to keep data
together that belongs together, condense your code, and perform the same methods and operations
on multiple values atonce.
The list data type has some more methods. Here are all of the
methods of list objects: list.append(x)
Add an item to the end of the list; equivalent to
a[len(a):] = [x]. list.extend(L)
Extend the list by appending all the items in the given list; equivalent to a[len(a):] = L.
list.insert(i, x)
Insert an item at a given position. The first argument is the index of the element before
which to insert, so a.insert(0, x) inserts at the front of the list, and a.insert(len(a), x) is
equivalent toa.append(x).
list.remove(x)
Remove the first item from the list whose value is x. It is an error if there
is no such item. list.pop([i])
Remove the item at the given position in the list, and return it. If no index is specified, a.pop() removes
and
returns the last item in the list. (The square brackets around the i in the method signature denote
that the parameter is optional, not that you should type square brackets at that position. You will
see this notation frequently in the Python Library Reference.)
list.index(x)
Return the index in the list of the first item whose value is x. It is an error if there
is no such item. list.count(x)
Return the number of times x appears in the list.
list.sort()
The list methods make it very easy to use a list as a stack, where the last element added is the first
element retrieved (“last-in, first-out”). To add an item to the top of the stack, use append(). To
retrieve an item from the top of the stack, use pop() without an explicit index.
It is also possible to use a list as a queue, where the first element added is the first element retrieved
(“first-in, first-out”); however, lists are not efficient for this purpose. While appends and pops from
the end of list are fast, doing inserts or pops from the beginning of a list is slow (because all of the
other elements have to be shifted by one).
To implement a queue, use collections.deque which was designed to have fast appends and pops
from both ends.
ENQUEUE
# Stack Implementation
def isEmpty(stk):
return len(stk) == 0
def Pop(stk):
if isEmpty(stk):
print("Underflow")
else:
item = stk.pop()
if len(stk) == 0:
top = None
else:
top = len(stk)
print("Popped item is " + str(item))
def Display(stk):
if isEmpty(stk):
print("Stack is empty")
else:
top = len(stk) - 1
print("Elements in the stack are:")
for i in range(top, -1, -1):
print(str(stk[i]))
if __name__ == "__main__":
stk = []
top = None
Push(stk, 1)
Push(stk, 2)
Push(stk, 3)
Push(stk, 4)
Pop(stk)
Display(stk)
# Queue Implementation
def enque(queue, data):
queue.insert(0, data)
def dequeue(queue):
if len(queue) > 0:
return queue.pop()
return "Queue Empty"
def display(queue):
print("Elements in the queue are:")
for i in range(len(queue)):
print(queue[i])
if __name__ == "__main__":
queue = []
enque(queue, 5)
enque(queue, 6)
enque(queue, 9)
enque(queue, 5)
enque(queue, 3)
print("Popped Element is: " + str(dequeue(queue)))
display(queue)
DEQUEUE
import collections
d = collections.deque('abcdefg')
print('Deque:', d )
print ('Length:', len(d))
print ('Left end:', d[0] )
print ('Right end:', d[-1] )
d.popleft()
print("Deque after popleft:",d)
d.pop()
print("Deque after pop :",d)
d.extendleft([0])
d.extend([6,7,8])
print(d)
R1 R2 R3 R4 Total Signature
(3 Marks) (3 Marks) (3 Marks) (1 Mark) (10 Marks)
EXPERIMENT NO: 11
Date of Performance :
Date of Submission :
THEORY:
Threading allows you to have different parts of your process run concurrently These different parts are
usually individual and have a separate unit of execution belonging to the same process. The process is
nothing but a running program that has individual units that can be run concurrently. For example, A web-
browser could be a process, an application running multiple cameras simultaneously could be a process;
a video game is another example of a process.
Inside a process comes the concept of multiple threading or commonly known as multi-threading, where
multiple threads work together to achieve a common goal. The most crucial benefit of using threads is that
it allows you to run the program in parallel.
Threading in Python
● In Python, the threading module is a built-in module which is known as threading and can be
directly imported.
● Since almost everything in Python is represented as an object, threading also is an object in
Python. A thread is capable of
o Holding data,
o Stored in data structures like dictionaries, lists, sets, etc.
o Can be passed as a parameter to a function.
To spawn another thread, you need to call following method available in thread module −
This method call enables a fast and efficient way to create new threads in both Linux and Windows.
The method call returns immediately and the child thread starts and calls function with the passed list of
args. When function returns, the thread terminates.
Here, args is a tuple of arguments; use an empty tuple to call function without passing any arguments.
kwargs is an optional dictionary of keyword arguments.
Example
#!/usr/bin/python
import
thread
import
time
w
Thread-1: Thu Jan 22 15:42:17 2009
hil
Thread-1: Thu Jan 22 15:42:19 2009
e
Thread-2: Thu Jan 22 15:42:19 2009
1:
Thread-1: Thu Jan 22 15:42:21 2009
pas
Thread-2: Thu Jan 22 15:42:23 2009
s
Thread-1: Thu Jan 22 15:42:23 2009
Thread-1: Thu Jan 22 15:42:25 2009
Thread-2: Thu Jan 22 15:42:27 2009
Thread-2: Thu Jan 22 15:42:31 2009
Thread-2: Thu Jan 22 15:42:35 2009
The newer threading module included with Python 2.4 provides much more powerful, high-level support
for threads than the thread module discussed in the previous section.
The threading module exposes all the methods of the thread module and provides some additional
methods −
In addition to the methods, the threading module has the Thread class that implements threading. The
methods provided by the Thread class are as follows −
● get() − The get() removes and returns an item from the queue.
● put() − The put adds item to a queue.
● qsize() − The qsize() returns the number of items that are currently in the queue.
● empty() − The empty( ) returns True if queue is empty; otherwise, False.
● full() − the full() returns True if queue is full; otherwise, False.
import threading
# Function to calculate volume of cube
def cube_volume(side):
volume = side ** 3
print(f"Volume of cube with side {side} is {volume}")
if __name__ == "__main__":
# Input side length
side = float(input("Enter the side length: "))
# Create threads
cube_thread = threading.Thread(target=cube_volume, args=(side,))
square_thread = threading.Thread(target=square_area, args=(side,))
# Start threads
cube_thread.start()
square_thread.start()
# Wait for threads to finish
cube_thread.join()
square_thread.join()
print("Calculation completed.")
R1 R2 R3 R4 Total Signature
(3 Marks) (3 Marks) (3 Marks) (1 Mark) (10 Marks)
EXPERIMENT NO: 12
Date of Performance :
Date of Submission :
THEORY:
The Python standard for database interfaces is the Python DB-API. Most Python database
interfaces adhere to this standard. Python Database API supports a wide range of database
servers such as −
● GadFly
● mSQL
● MySQL
● PostgreSQL
● Informix
● Interbase
● Oracle
● Sybase
The DB API provides a minimal standard for working with databases using Python structures
and syntax wherever possible. This API includes the following −
● Closing theconnection
MySQLdb
MySQLdb is an interface for connecting to a MySQL database server from Python. It implements
the Python Database API v2.0 and is built on top of the MySQL C AP
Database Connection
Before connecting to a MySQL database, make sure of the followings −
If a connection is established with the datasource, then a Connection Object is returned and saved
into db for further use, otherwise db is set to None. Next, db object is used to create a cursor
object, which in turn is used to execute SQL queries. Finally, before coming out, it ensures that
database connection is closed and resources are released.
Example
Let us create Database table EMPLOYEE −
#!/usr/bin/python
import MySQLdb
# Open database connection
db = MySQLdb.connect("localhost","testuser","test123","TESTDB" )
cursor = db.cursor()
# Drop table if it already exist using execute() method.
cursor.execute("DROP TABLE IF EXISTS EMPLOYEE")
# Create table as per requirement
sql = """CREATE TABLE EMPLOYEE (
FIRST_NAME CHAR(20) NOT NULL,
LAST_NAME CHAR(20),
AGE INT,
SEX CHAR(1),
INCOME FLOAT )"""
cursor.execute(sql)
# disconnect from server
db.close()
INSERT Operation
It is required when you want to create your records into a database table.
EMPLOYEE(FIRST_NAME, LAST_NAME,
VALUESREAD
('Mac',Operation
'Mohan', 20, 'M', 2000)"""
READ Operation on any database means to fetch some useful information from the database.
Once our database connection is established, you are ready to make a query into this database.
You can use either fetchone() method to fetch single record or fetchall() method to fetech
multiple values from a database table.
● fetchone() − It fetches the next row of a query result set. A result set is an object that is
returned when a cursor object is used to query atable.
● fetchall() − It fetches all the rows in a result set. If some rows have already been
extracted from the result set, then it retrieves the remaining rows from the resultset.
● rowcount − This is a read-only attribute and returns the number of rows that were
affected by an execute()method.
(after update):
(after delete):
R1 R2 R3 R4 Total Signature
(3 Marks) (3 Marks) (3 Marks) (1 Mark) (10 Marks)