Unit - I Data Engineering With Python (Data Science)
Unit - I Data Engineering With Python (Data Science)
G College
Narayanaguda –Hyderabad
Name : ________________________________
Roll No : ________________________________
Group : ________________________________
1. Write programs to parse text files, CSV, HTML, XML and JSON documents and extract
relevant data. After retrieving data check any anomalies in the data, missing values etc.
2. Write programs for reading and writing binary files
3. Write programs for searching, splitting, and replacing strings based on pattern matching
using regular expressions
4. Design a relational database for a small application and populate the database. Using SQL
do the CRUD (create, read, update and delete) operations.
5. Create a Python MongoDB client using the Python module pymongo. Using a collection
object practice functions for inserting, searching, removing, updating, replacing, and
aggregating documents, as well as for creating indexes.
6. Write programs to create numpy arrays of different shapes and from different sources,
reshape and slice arrays, add array indexes, and apply arithmetic, logic, and aggregation
functions to some or all array elements.
7. Write programs to use the pandas data structures: Frames and series as storage containers
and for a variety of data-wrangling operations, such as:
• Single-level and hierarchical indexing
• Handling missing data
• Arithmetic and Boolean operations on entire columns and tables
• Database-type operations (such as merging and aggregation)
• Plotting individual columns and whole tables
• Reading data from files and writing data to files
2
Chapter 1
Data Science
• The first step in the data analysis process is to define the objectives , understanding the
problem , identifying the data needed to address it, and defining the metrics to measure
the outcomes.
• The next step is to collect the relevant data. This can be done through various methods
such as surveys, interviews, observations, or extracting from existing databases. The data
collected can be quantitative (numerical) or qualitative (non-numerical)
3
• Data cleaning, It involves checking the data for errors and inconsistencies, and correcting
or removing them.
• Once the data is cleaned, it's time for the actual analysis. This involves applying statistical
or mathematical techniques to the data to discover patterns, relationships, or trends.
There are various tools and software available for this purpose, such as Python, R, Excel,
etc.
• The next step is to interpret the results and visualize them in a way that is easy to
understand.
• The final step in the data analysis process is data storytelling. It is crucial for
communicating the results to non-technical audiences and for making data-driven
decisions.
Types of Data Analysis :
Data analysis can be categorized into four main types, each serving a unique purpose and
providing different insights. These are descriptive, diagnostic, predictive, and prescriptive
analyses.
4
1.2 Data Acquisition Pipeline
The 3 main sources of data are Databases, Internet and Local files. These data obtained may be
structured or unstructured.
The most popular data formats are :
➢ Unstructured plain text in a natural language (Such as English or Chinese)
➢ Structured data includes Tabular data in CSV (Comma separated values) files, Tabular
data from databases, tagged data in HTML (Hyper Text Markup Language), XML
(eXtensible Markup Language), Tagged data in JavaScript Object Notation (JSON).
➢ Original structure of the extracted data is represented using native python data
structures (lists and dictionaries) or advanced data structures that support specialized
(arrays and data frames).
➢ Data pipeline automation converts data from various sources into a specific format to
be stored or for other analysis tools.
5
1.3 Report Structure
The project report data scientists submit to the data sponsor (the customer) . The
report typically includes the following :
➢ Abstract (a brief description of the project)
➢ Introduction
➢ Methods that were used for data acquisition and processing
➢ Results that were obtained while processing
➢ Conclusion
➢ Appendix
In addition to the non-essential results and graphics, the appendix contains all
reproducible code used to process the data.
6
Chapter 2
Files and Working with Text Data
2.1 Introduction to Files
• A file is a named location on a secondary storage media where data are permanently
stored for later access.
• All the data on hard drive consists of files and directories. Files store data, Directories
store files and other directories.
• The folders often referred to as directories, are used to organize files on computer.
• The Directories themselves take up virtually no space on the hard drive. Files, on the
other hand, can range from a few bytes to several gigabytes.
Example:
• Web standards: html, XML, CSS, JSON, etc.
• Source code: c, cpp, js, py, java, etc.
• Documents: txt, tex, rtf,asciidoc, etc.
• Tabular data: csv, tsv , etc.
7
Example:
• Document files: .pdf, .doc, .xls ,.ppt, etc.
• Image files: .png, .jpg, .gif, .bmp etc.
• Video files: .mp4, .3gp, .mkv, .avi etc.
• Audio files: .mp3, .wav, .mka, .aac etc.
• Database files: .mdb, .accde, .frm, .sqlite etc.
• Archive files: .zip, .rar, .iso, .7z etc.
• Executable files: .exe, .dll, .class etc.
All binary files follow a specific format. We can open some binary files in the normal text
editor but we can’t read the content present inside the file. That’s because all the binary files
will be encoded in the binary format, which can be understood only by a computer or
machine.
For handling such binary files we need a specific type of software to open it.
For Example, You need Microsoft word software to open .doc binary files. Likewise, you need
a pdf reader software to open .pdf binary files and you need a photo editor software to read
the image files and so on.
8
2.2.3 Fully Qualified Path and Relative Path
• A file path can be fully qualified path name or relative path.
• The fully qualified path name is also called an Absolute path. Absolute path is the exact
address of the file in the file system, starting from the root.
Example : c:\Students\MyFolder\[Link] refers to file name [Link] in
under root directory c:\
• A Relative path starts with respect to present working directory.
Example : \MyFolder\[Link] refers to file name [Link] in under present
working directory MyFolder
Here, file_name is the name of the file or the location of the file that you want to open,
and file_name should have the file extension included as well.
Which means in [Link] – the term test is the name of the file and .txt is the extension
of the file.
The mode in the open function syntax will tell Python as what operation you want to do on a
file.
• ‘r’ – Read Mode: Read mode is used only to read data from the file.
• ‘w’ – Write Mode: This mode is used when you want to write data into the file or modify
it. Remember write mode overwrites the data present in the file.
• ‘a’ – Append Mode: Append mode is used to append data to the file. Remember data
will be appended at the end of the file pointer.
• ‘r+’ – Read or Write Mode: This mode is used when we want to write or read the data
from the same file.
• ‘a+’ – Append or Read Mode: This mode is used when we want to read data from the file
or append the data into the same file.
9
2.3.2 Closing a File
In order to close a file, we must first open the file. In python, we have an in-built method
called close() to close the file which is opened.
Whenever you open a file, it is important to close it, especially, with write method. Because
if we don’t call the close function after the write method then whatever data we have written
to a file will not be saved into the file.
Description
Method Syntax
10
The tell() method returns the
tell() file_object.tell()
current position within the file.
f=open("E:/[Link]","w")
[Link]("Hello World.\nHello Python")
[Link]()
f=open("E:/[Link]","r")
print([Link]())
[Link]()
11
Example Program on write and read method with specific size :
f=open("E:/[Link]","w")
[Link]("Hello World.\nHello Python")
[Link]()
f=open("E:/[Link]","r")
print([Link](5))
[Link]()
f=open("E:/[Link]","w")
lines = ["Hello everyone\n", "Writing multiline strings\n",
"This is the third line"]
[Link](lines)
[Link]()
f=open("E:/[Link]","r")
print([Link]())
[Link]()
f=open("E:/[Link]","w")
lines = ["Hello everyone\n", "Writing multiline strings\n",
"This is the third line"]
[Link](lines)
[Link]()
f=open("E:/[Link]","r")
print([Link](7))
[Link]()
12
Example Program on writelines and readlines method :
f=open("E:/[Link]","w")
lines = ["Hello everyone\n", "Writing multiline
strings\n", "This is the third line"]
[Link](lines)
[Link]()
f=open("E:/[Link]","r")
print([Link]())
[Link]()
f=open("E:/[Link]","w")
lines = ["Hello everyone\n", "Writing multiline strings\n",
"This is the third line"]
[Link](lines)
[Link]()
f=open("E:/[Link]","r")
d=[Link]()
for line in d:
words=[Link]()
print(words)
[Link]()
13
Example Program on splitlines method :
splitlines() function /method is used to display each line separately as an element of a lsit.
f=open("E:/[Link]","w")
lines = ["Hello everyone\n", "Writing multiline strings\n",
"This is the third line"]
[Link](lines)
[Link]()
f=open("E:/[Link]","r")
d=[Link]()
for line in d:
words=[Link]()
print(words)
[Link]()
14
2.3.4 Opening a file using with clause / Statements
In Python, we can also open a file using with clause . The syntax of with clause is:
with open (file_name, access_mode) as file_ object:
• The advantage of using with clause is that any file that is opened using this clause is
closed automatically, once the control comes outside the with clause.
• In case the user forgets to close the file explicitly or if an exception occurs, the file is
closed automatically.
def writefile():
with open("E:/[Link]", "w") as f:
[Link]("Python is a general-purpose high-level
language")
def readfile():
with open("E:/[Link]", "r") as f:
print([Link]())
def main():
writefile()
readfile()
if __name__=="__main__":
main()
15
2.4 Reading and Writing Binary Files
Access modes in Binary files.
• ‘wb’ – Open a file for write only mode in the binary format.
• ‘rb’ – Open a file for the read-only mode in the binary format.
• ‘ab’ – Open a file for appending only mode in the binary format.
• ‘rb+’ – Open a file for read and write only mode in the binary format.
• ‘ab+’ – Open a file for appending and read-only mode in the binary format.
Example 1:
f = open("E:/[Link]", "wb")
num=[10,20,30,40,50]
arr=bytearray(num)
[Link](arr)
f = open("E:/[Link]", "rb")
print([Link]())
Example 2:
f = open("E:/[Link]", "wb+")
message = "Hello Python"
file_encode = [Link]("ASCII")
[Link](file_encode)
[Link](0)
bindata = [Link]()
print("Binary Data:",bindata)
txtdata = [Link]("ASCII")
print("Normal data:", txtdata)
16
2.5 The Pickle Module
• Python objects (list, tuple, dictionary, etc ) can be serialized into binary form and
deserialized back to Python objects using the methods and classes available from the
Python module pickle.
• Serialization or pickling is the process of converting an object in memory to a byte stream
that can be stored on disk or sent over a network.
• De-serialization or unpickling is the inverse of pickling process where a byte stream is
converted back to Python object.
• The pickle module deals with binary files. The pickle module provides two methods -
dump() and load() to work with binary files for pickling and unpickling,
respectively.
where data_object is the object that has to be dumped to the file with the file handle
named file_object.
17
Example program on dump() and load():
import pickle
with open("[Link]","wb") as f:
list=[10,"Santhosh",1992]
dic={"Subject":"Python","Exp":10}
[Link](list,f)
[Link](dic,f)
with open("[Link]", "rb") as f:
lst=[Link](f)
d=[Link](f)
print(lst)
print(d)
18
CSV File Structure
2.6.1 writer()
• To write to a CSV file in Python, we can use the [Link]() function.
• The [Link]() function returns a writer object that converts the user's data into
a delimited string.
• The writer class has following methods:
writerow() :- This function writes items in a sequence (list, tuple or string) separating
them by comma character (i.e. Write a single line)
writerows() :- This function writes each sequence in a list as a comma separated line
of items in the file (i.e. Write Multiple lines)
Note :
Problem with writer() method is extra newline will appear after each line in csv file.
To remove this we have to open the csv file with newline parameter as empty string (‘ ‘).
2.6.2 reader()
• This function returns a reader object which is an iterator of lines in the csv file. We can
use a for loop to display lines in the file. The file should be opened in 'r' mode.
19
Example program on writerow() and reader()
import csv
row=['Santhosh','Faculty','10']
f=open("[Link]",'w', newline='')
wobj=[Link](f)
[Link](row)
[Link]()
f=open("[Link]",'r')
robj=[Link](f)
for data in robj:
print(data)
[Link]()
import csv
rows=[['Santhosh','Java','5'],
['Santhosh','Data Structure','3'],
['Santhosh','Python','2']]
f=open("[Link]",'w',newline='')
wobj=[Link](f)
[Link](rows)
[Link]()
f=open("[Link]",'r')
robj=[Link](f)
for data in robj:
print(data)
[Link]()
20
Example program on csv file :
import csv
f=open("[Link]","w",newline='')
swriter=[Link](f)
[Link](['Rollno','Name','Marks'])
rec=[]
while True:
r=int(input("Enter Roll no : "))
n=input("Enter Name : ")
m=int(input("Enter Marks : "))
list=[r,n,m]
[Link](list)
ch=input("Do you want to enter more records : (y/n)")
if ch=='n':
break
for i in rec:
[Link](i)
[Link]()
f=open("[Link]","r")
sreader=[Link](f)
print("Student Details are : ")
for i in sreader:
print(i)
[Link]()
21
2.6.3 writing and reading into a file using Dictionary
• The dictionary data can be read and written to a csv file using DictReader() and
DictWriter() classes.
Syntax of DictReader :
[Link](fileobj,fieldnames=None,restkey=None)
Syntax of DictWriter :
[Link](fileobj,fieldnames)
import csv
my_dictionary=[{'name':'santhosh','subject':'java','Semester':5},
{'name':'santhosh','subject':'Python','Semester':2},
{'name':'santhosh','subject':'C++','Semester':2}]
my_attributes=['name','subject','Semester']
with open('[Link]','w',newline='') as f:
wobj=[Link](f,fieldnames=my_attributes)
[Link]()
[Link](my_dictionary)
with open('[Link]','r') as f:
robj=[Link](f)
for i in robj:
print(i)
22
Example Program to display specific key values using DictReader() and
DictWriter() .
import csv
my_dictionary=[{'name':'santhosh','subject':'java','Semester':5},
{'name':'santhosh','subject':'Python','Semester':2},
{'name':'santhosh','subject':'C++','Semester':2}]
my_attributes=['name','subject','Semester']
with open('[Link]','w',newline='') as f:
wobj=[Link](f,fieldnames=my_attributes)
[Link]()
[Link](my_dictionary)
with open('[Link]','r') as f:
robj=[Link](f)
for i in robj:
print(i['subject'])
23
2.7.1 Various methods of os Module
24
3. followlinks − If followlinks is true, this
navigates to directories that our mentioned
links point to.
This is optional. By default, it is set
to false.
25
Example Program on os module methods
import os
print([Link]())
[Link]("E:\Avanthi")
[Link]("E:\Avanthi")
print([Link]())
print([Link]("E:"))
[Link]("..")
[Link]("E:\Avanthi")
26
relpath() [Link](path, This method returns a relative filepath to path
start=[Link]) either from the current directory or from an
optional start directory.
dirname() [Link](path) This method returns the directory name of
the pathname path.
basename() [Link](path) This method returns the base name of
pathname path.
split() [Link](path) This method splits the pathname path into a
pair, (head, tail)
splitext() [Link](path) This method splits the pathname path into a
pair (root, ext) such that
root + ext == path where ext begins with a
period and contains at most one period and
root is everything leading up to that.
getsize() [Link](path) This method returns the size, in bytes, of
path.
import os
filename = 'BSC/MSDS/[Link]'
print("split :",[Link](filename))
print("splitext :",[Link](filename))
print("Directory name :",[Link](filename))
print("Base name :",[Link](filename))
print("join",[Link]([Link](filename),
[Link](filename)))
print("Relative Path: ",[Link](filename))
print("Absoulute Path: ",[Link](filename))
print("isdir : ",[Link](filename))
27
Consider the File Structure Given Below. Write Python Program to Delete All the Files and
Subdirectories from the main (i.e. Data Science) Directory.
import os
def deletefiles(path):
for root,dirs,files in [Link](path):
for file in files:
file_path=[Link](root,file)
print(file_path," is deleted")
[Link](file_path)
def main():
path=input("Enter the directory path you want to delete files")
deletefiles(path)
if __name__=="__main__":
main()
28
Chapter 3
Working with Text Data
3.1 JSON and XML in Python
• JSON (JavaScript Object Notation) and XML (Extensible Markup Language) standards are
commonly used for transmitting data in web applications.
• The web is based on very basic client/server architecture , client (usually a web browser )
sends a request to a server, using Hypertext Transfer Protocol (HTTP). The server answers
the request using the same protocol.
HTTP(S)
29
JSON serialization and deserialization are the processes of converting JSON data to
and from other formats, such as Python objects or strings, to transmit or store the data.
3.1.1 Python to JSON (Encoding / Serializing)
• Serialization / Encoding is the process of converting an object or data structure into a
JSON string.
• Here are some common functions from json library that are used for serialization :
dumps() and dump()
[Link]()
This function is used to serialize a Python object into a JSON string. The dumps() function
takes a single argument, the Python object, and returns a JSON string
Syntax :
json_string = [Link](python_obj)
[Link]()
This function is used to serialize a Python object and write it to a JSON file. The dump()
function takes two arguments, the Python object and the file object.
Syntax :
[Link](python_obj,file_object)
[Link]()
This function is used to parse a JSON string into a Python object. The loads() function
takes a single argument, the JSON string, and returns a Python object.
Syntax :
python_obj = [Link](json_string)
[Link]()
This function is used to read a JSON file and parse its contents into a Python object. The
load() function takes a single argument, the file object, and returns a Python object.
Syntax :
python_obj = [Link](file_object)
30
Program to demonstrate Python Serializing using JSON dumps() method and
Deserializing using JSON loads() method.
import json
python_obj = {"name":"Santhosh",
"Subject":"Data Engineering with Python",
"Semester":3}
json_string = [Link](python_obj)
python_obj = [Link](json_string)
print(python_obj)
import json
python_obj = {"name":"Santhosh",
"Subject":"Data Engineering with Python",
"Semester":3}
with open("[Link]",'w') as file:
[Link](python_obj, file)
with open('[Link]', 'r') as file:
python_obj = [Link](file)
print(python_obj)
31
Formatting JSON Data
In Python, the [Link]() function provides options for formatting and ordering the JSON
output. Here are some common options:
1. Indent
This option specifies the number of spaces to use for indentation in the output JSON string.
2. sort_keys
This option specifies whether the keys in the output JSON string should be sorted in
alphabetical order
import json
python_obj = {"name":"Avanthi",
"age":1991,
"semester":"Three"}
json_string =
[Link](python_obj,indent=2,sort_keys=True)
print(json_string)
32
3.1.3 Using Requests Module
• The requests module allows you to send HTTP requests using Python.
• The system that sends requests is known as the client, and the system that holds the
webserver is known as a server.
• The HTTP request returns a Response Object with all the response data (content,
encoding, status, etc).
• While working with the requests, we will come across the following methods.
get - It is used to request data from a server.
post - It is used to submit some data to the server for processing it.
• The response object will store the information. Below are the essential properties.
content - It returns the content of the data for the responses.
status_code - It returns the status of our request. For example - 200 OK means you
made successful request, 404 NOT FOUND means resource is not
found.
To install Requests, simply run this simple command in command prompt
C:\>pip install requests
To import module in pycharm
Type the name of the package (import requests) and hit Alt-Enter , then choose Install
and import package
Program to get Text Response content, status_code and url response using
requests module
import requests
resp = [Link]('[Link]
print(resp.status_code)
print([Link])
print([Link])
33
Program to get JSON response content using requests module
import requests
response = [Link]('[Link]
print(response)
print([Link]()) #returns a JSON object of the result
Introduction to ElementTree
Python has a built in library, ElementTree, that has functions to read and manipulate XMLs (and
other similarly structured files). ElementTree module available in [Link] package.
First, import ElementTree. It's a common practice to use the alias of ET:
import [Link] as ET
Syntax of parse() :
ElementTree_Object = [Link]('[Link]')
[Link]() function takes the file name as an argument and returns an ElementTree object.
35
Program 1 : Construct an XML File and Write Python Program to
parse the XML Data
[Link]
<student>
<name type="item1">Avanthi</name>
<Rollno type="item2">1991</Rollno>
</student>
[Link]
import [Link] as ET
tree = [Link]('[Link]')
root = [Link]()
print(root)
Syntax of fromstring() :
root = [Link](xml_string)
The fromstring() function returns the root Element of the parsed XML tree.
import [Link] as ET
root=[Link]('''<Student>
<name>Avanthi</name>
<rollno>1991</rollno>
</Student>''')
print("Root element name : ",[Link])
print("Sub element contents are :")
for elem in root:
print([Link])
print("Sub element contents are :")
for content in root:
print([Link])
36
Program 3 : Write a Python program to Generate XML formatted Data and
Save it as XML Document and displays tag names.
import [Link] as ET
root=[Link]("subjects")
child=[Link](root,"semester")
subchild_1=[Link](child,"first",{"name":"sub1"})
subchild_2=[Link](child,"second",{"name":"sub2"})
subchild_1.text="FIT"
subchild_2.text="Python"
tree=[Link](root)
[Link]("[Link]")
content=[Link]("[Link]")
data=[Link]()
print(data) #returns root element
print([Link]) #returns root tag name
print(data[0].tag) #returns child tag name
print(data[0][0].tag) # returns subchild1 tag name
print(data[0][1].tag) # returns subchild2 tag name
37
[Link]
Unit-I Questions
1. Explain about data analysis sequence
2. What is Data Acquisition? Explain about data acquisition pipeline
3. Write a short note on Data Science and report structure
4. What is File? Explain different types of files
5. Write short note on File paths and File methods to reading and writing data
in Python.
6. Working with Text files with example programs
7. Working with Binary files with example programs
8. Explain about pickle module with an example program.
9. Working with CSV files with example programs.
10. os Module & [Link] module methods with an example programs.
11. Working with JSON file with example programs.
12. Working with XML files with example programs.
38