Working with Data in Python
Reading, Writing, Pandas, Numpy
Zia Ul Rehman
Reading Files with Open
• We will use Python's built-in open function to create a file
object, and obtain the data from a "txt" file.
• We can apply a method to that object to read data from the
file.
Reading Files with Open
Reading Files with Open
Reading Files with Open
• We can print the file content. We will see the following.
• When we examine the raw string, we will see the "\n." This is so
Python knows to start a new line.
Reading Files with Open
Reading Files with Open
Reading Files with Open
Reading Files with Open
Reading Files with Open
• Each time we call the method, we will progress through the text.
Writing Files with Open
• We can also write to files using the open function.
• We can apply method write to write data to that file. As a result, text
will be written to the file.
• We set the mode parameter to W for writing.
Writing Files with Open
Writing Files with Open
Writing Files with Open
Loading Data with Pandas
• We can import the library or a dependency like Pandas using the
following command.
• We now have access to a large number of pre-built classes and
functions.
Loading Data with Pandas
Loading Data with Pandas: Dataframes
• A data frame is comprised of rows and columns.
• We can create a data frame out of a dictionary. The keys correspond
to the column labels. The values or lists corresponding to the rows.
• We then cast the dictionary to a data frame using the function data
frame.
Loading Data with Pandas: Dataframes
Loading Data with Pandas: Dataframes
Loading Data with Pandas: Dataframes
Loading Data with Pandas: Dataframes
Loading Data with Pandas: Dataframes
Loading Data with Pandas: Dataframes
Pandas: Working with and Saving Data
Pandas: Working with and Saving Data
Pandas: Working with and Saving Data
Pandas: Save as CSV
Numpy
• Numpy is a library for scientific computing. It has many
useful functions.
• There are many other advantages like speed and memory.
• Objectives:
• covering the basics and array creation
• indexing and slicing
• basic operations
• universal functions.
The Basics & Array Creation
• A Python list is a container that allows you to store and access data.
Each element is associated with an index, we can access each
element using a square bracket as follows:
The Basics & Array Creation
• A numpy array or ndarray is similar to a list. It's usually fixed in
size, and each element is of the same type.
• We can cast a list to an numpy array by first importing numpy.
• We can access the data via an index. As with the lists, we can
access each element with an integer and a square bracket.
• If we check the type of the array we get, [Link].
• As numpy arrays contain data of the same type, we can use the
attribute dtype to obtain the data type of the array's elements.
• Let's review some basic array attributes using the array "a."
• The attribute size is the number of elements in the array, as
there are five elements the result is five.
• The attribute ndim represents the number of array
dimensions or the rank of the array.
• The attribute shape is a tuple of integers indicating the size
of the array in each dimension.
• We can create a numpy array with real numbers.
• When we check the type of the array, we get [Link].
• If we examine the attribute dtype, we see "float64" as the elements
are not integers.
• There are many other attributes. Check out [Link]
Indexing and Slicing
• We can change the first
element of the array to a 100 as
follows.
• The arrays first value is now a 100.
• We can change the fifth
element of the array as follows.
• The fifth element is now zero.
Indexing and Slicing
• Like lists and tuples, we can slice
a numpy array.
• We can select the elements from
one to three, and assign it to a
new numpy array "d" as follows.
The elements in "d" correspond
to the index.
• Like lists, we do not count the
element corresponding to the
last index.
Indexing and Slicing
• We can assign the corresponding indices to new values as
follows. The array "c" now has new values.
Indexing and Slicing
• Consider the following line of code What is the value of c[0]
Basic Operations
• Numpy makes it easier to do many operations that are
commonly performed in data science.
• These same operations are usually computationally faster
and require less memory in numpy compared to regular
python.
• Let's review some of these operations on one dimensional
arrays.
Basic Operations: Vector Addition and
Subtraction
Basic Operations: Vector Addition and
Subtraction
Basic Operations: Vector Addition and
Subtraction
Basic Operations: Array multiplication with a
Scalar
Basic Operations: Array multiplication with a
Scalar
Basic Operations: Array multiplication with a
Scalar
Product of two Numpy arrays
Product of two Numpy arrays
Dot Product
Dot Product
• We can also perform dot
product using the numpy
function dot, and assign it
with the variable result as
follows.
Adding Constant to an Numpy Array
(Broadcasting)
Universal Function
Universal Function
• We can find the maximum value
using the method five.
• We see the largest value is five.
Therefore, the method max
returns of five.
Universal Function
2-Dimensional Numpy Arrays
• The Basics and Array Creation in 2D
• Indexing and Slicing in 2D
• Basic Operations in 2D
The Basics and Array Creation in 2D
The Basics and Array Creation in 2D
The Basics and Array Creation in 2D
• what is the shape of the following array:
• A=[Link]([[1,0,1],[0,1,1]])
The Basics and Array Creation in 2D
The Basics and Array Creation in 2D
The Basics and Array Creation in 2D
• consider the following array:
• A=[Link]([[1,0,1],[2,2,2]])
• what is the value in A[0,1]
Slicing
Matrix Addition
Matrix Addition
Multiply with a scalar
Product of Matrix
Matrix Multiplication
Matrix Multiplication