Python Basics for Data Science & AI
Python Basics for Data Science & AI
Introduction
This course was designed to provide the building blocks for Python programming and data
collection for those choosing a career in Data Analysis, Data Science, Data Engineering, AI or
Application Development.
Initially conceived as a foundation course for Data Science and AI it has been refreshed several
times to keep pace with emerging career options. Additional content has been added which is
applicable to Data Science, Data Engineering, AI or Application Development.
After completing this course you will have learned foundational skills in Python programming
which you can then go on to apply in the Python Project course for your chosen career. The
Python Project courses involve real world scenarios where you are in charge of a final project as
a Data Scientist, a Data Engineer, or in AI and Application Development. By finishing this
course and your follow-on Python Project, you will gain the basic skills to continue the steps on
your chosen career path.
Python is a dynamically typed language, which means you don't need to explicitly declare the
data type of a variable. However, Python has built-in data types that represent the various types
of values you can work with. Here are some common data types in Python:
Numeric Types:
1
Sequence Types:
Set Types:
Mapping Type:
Boolean Type:
None Type:
2
NoneType: Represents the absence of a value, similar to null in other languages.
In Python, type casting or type conversion refers to the process of converting a variable from one
data type to another. Python provides several built-in functions for type casting. Here are some
commonly used type casting functions:
In Python, expressions are combinations of values and operators that can be evaluated to produce
a result. Expressions can include literals, variables, operators, and function calls. Let's break
down some key elements:
4
Variables: Variables are names given to memory locations where you store values. They are
used to reference and manipulate data in your program.
When naming variables in Python, it's important to follow certain rules to ensure clarity,
readability, and adherence to the language syntax. Here are the rules for naming variables in
Python:
Valid Characters:
Variable names can only contain letters (a-z, A-Z), digits (0-9), and underscores (_).
Case Sensitivity:
Python is case-sensitive. This means that variables with different letter cases are treated as
distinct. For example, myVar and myvar are two different variables.
Reserved Words:
Avoid using Python keywords as variable names. Examples of keywords include if, else, for,
while, True, False, None, and others. Using these words as variable names may lead to confusion
and errors.
5
Convention for Style:
According to PEP 8:
Use descriptive and meaningful names to convey the purpose of the variable.
While starting a variable name with an underscore is technically allowed, it is often reserved for
special cases in Python. Avoid using a single leading underscore in regular variable names.
Variable names with double underscores at the beginning and end (sometimes referred to as
"dunder" names) are typically reserved for special methods or attributes in Python classes.
Operators
Operators perform operations on variables and values. Python supports various operators,
including arithmetic, comparison, logical, and more.
6
Expressions: Expressions are combinations of literals, variables, and operators that can be
evaluated to produce a result.
Arithmetic operation
Python provides several arithmetic operators for performing common mathematical operations.
Here are the main arithmetic operators in Python:
Addition (+):
Subtraction (-):
Multiplication (*):
7
Division (/):
In Python 3, the division operator (/) always returns a floating-point result, even if the division is
exact.
The floor division operator returns the largest integer less than or equal to the division result.
Exponentiation (**):
The exponentiation operator raises the left operand to the power of the right operand.
8
These operators can be used in combination within expressions to perform more complex
calculations. For example:
It's important to be aware of the order of operations in expressions, and you can use parentheses
to specify the order if needed.
Function Calls: Function calls can also be part of expressions. Functions take input arguments,
perform some operation, and return a result.
String Operation
Strings in Python are sequences of characters, and Python provides several built-in functions and
operators for working with strings. A string can be enclose in single or double quote. It can
contain text, numbers or special characters. A string can be access using index. The first index of
the array is 0 and to access the array element you write the array name then followed by the
index number.
The index of a string can be numbered from right to left. Doing so will make the First element of
the string to the right to be -1 as shown below.
9
Here are some common string operations in Python:
Concatenation (+):
Repetition (*)
Strings can be indexed to access individual characters, and slicing can be used to extract
substrings.
10
String Stride
Length (len()):
String Methods:
11
lower(): Converts all characters to lowercase.
upper(): Converts all characters to uppercase.
strip(): Removes leading and trailing whitespaces.
replace(old, new): Replaces occurrences of a substring with another substring.
find(substring): Returns the index of the first occurrence of a substring (or -1 if not
found).
count(substring): Returns the number of occurrences of a substring.
Split(separator): Splits the string into a list of substrings based on the given separator.
Formatting Strings:
Python provides different ways to format strings, including the old % formatting, the [Link]()
method, and the more recent f-strings.
Example 1
name = "Alice"
age = 25
Example 2
name = "Bob"
age = 30
Example 3
12
name = "Charlie"
age = 35
\ are meant to proceed escape sequences. Escape sequences are strings that are difficult to input
Example
\t represent a tab. To place a back slash in your string use a double backslash or r in the front of
the string.
13
List and tuples
lists and tuples are both data structures used to store ordered collections of items. They have
some similarities, but there are key differences between them. Here's an overview of lists and
tuples:
Lists:
Mutable: Lists are mutable, meaning you can change their elements (add, remove, or
modify) after the list is created.
Example:
List Operations:
Use Case: Use lists when you need a collection of items that may change during the lifetime of
the program, and you want to perform various operations on them.
Tuples:
Immutable: Tuples are immutable, meaning once they are created, you cannot change their
elements. However, you can create a new tuple with modified elements.
14
Syntax: Defined using parentheses ().
Example:
Operations:
Use Case: Use tuples when you have a collection of items that should remain constant
throughout the program, and you don't need to modify them.
Indexing and Slicing: Both lists and tuples support indexing and slicing to access elements.
Length (len()):
Iteration
Membership Test
15
Note: We can sort element of a tuple. Using the sort method.
Nesting tuples
Summary on list
Imagine you received album recommendations from your friends and compiled all of the
recommandations into a table, with specific information about each album.
The table has one row for each movie and several columns:
16
Genre - Genre of the album
Lists
Indexing
We are going to take a look at lists in Python. A list is a sequenced collection of different objects
such as integers, strings, and even other lists as well. The address of each element within a list is
called an index. An index is used to access and refer to items within a list.
17
To create a list, type the list within square brackets [ ], with your content inside the parenthesis
and separated by commas.
18
List Content
Lists can contain strings, floats, and integers. We can nest other lists, and we can also nest tuples
and other data structures. The same indexing conventions apply for nesting:
List Operations
We can also perform slicing in lists. For example, if we want the last two elements, we use the
following command:
19
20
Copy and Clone List
When we set one variable B equal to A, both A and B are referencing the same list in memory:
Initially, the value of the first element in B is set as "hard rock". If we change the first element in
A to "banana", we get an unexpected side effect. As A and B are referencing the same list, if we
change list A, then list B also changes. If we check the first element of B we get "banana" instead
of "hard rock":
21
You can clone list A by using the following syntax:
22
Exercise
Create a list a_list, with the following elements 1, hello, [1,2,3] and True.
At first we need to create a empty list for storing the items to buy in Shopping list.
Watch
Laptop
Shoes
Pen
Clothes
Seems like I missed one item "Football" to add in the shopping list.
23
Task-6 Print the entire Shopping List
Task-7 Print the item that are important to buy from the Shopping List
Instead of "Pen" I want to buy "Notebook" let's change the item stored in the list.
Task-9 Delete the item from the shopping_list that is not required
Let's delete items that are unimportant, such as; I don't want to buy Clothes, let's delete it.
Summary on tuples
Tuples
In Python, there are different data types: String, Integer, and Float. These data types can all be
contained in a tuple as follows:
Now, let us create your first tuple with string, integer and float.
24
Indexing
Each element of a tuple can be accessed via an index. The following table represents the
relationship between the index and the items in the tuple. Each element can be obtained by the
name of the tuple followed by a square bracket with the index number:
25
26
27
Evaluation
28
Access the element, with respect to index 3
Dictionary
A dictionary is a built-in data type that represents a collection of key-value pairs. Each key must
be unique within a dictionary, and it maps to a corresponding value. Dictionaries are also known
as associative arrays, hash maps, or hash tables in other programming languages.
Creating dictionary
my_dict = {}
person = {
"age": 30,
29
# Another way to create a dictionary using the dict() constructor
Accessing values
Removing elements
Dictionary Methods:
30
values(): Returns a list of all values in the dictionary.
items(): Returns a list of tuples, where each tuple contains a key-value pair.
31
Summary on Dictionary
A dictionary consists of keys and values. It is helpful to compare a dictionary to a list. Instead of
being indexed numerically like a list, dictionaries have keys. These keys are the keys that are
used to access values within a dictionary.
The best example of a dictionary can be accessing person's detais using the social security
number.
Here the social security number which is a unique number will be the key and the details of the
people will be the values associated with it.
32
Create a Dictionary and access the elements
An example of a Dictionary Dict: Here we are creating a dictionary named Dict with he
following details
33
In summary, like a list, a dictionary holds a sequence of elements. Each element is represented
by a key and its corresponding value. Dictionaries are created with two curly braces containing
keys and values separated by a colon. For every key, there can only be one single value,
however, multiple keys can hold the same value. Keys can only be strings, numbers, or tuples,
but values can be any data type.
It is helpful to visualize the dictionary as a table, as in the following image. The first column
represents the keys, the second column represents the values.
34
35
36
Evaluation
The Albums Back in Black, The Bodyguard and Thriller have the following music recording
sales in millions 50, 50 and 65 respectively:
a) Create a dictionary album_sales_dict where the keys are the album name and the sales in
millions are the values.
c) Find the names of the albums from the dictionary using the method keys():
d) Find the values of the recording sales from the dictionary using the method values:
Scenario:Inventory Store
The inventory store scenario project utilizes a dictionary-based approach to develop a robust
system for managing and tracking inventory in a retail store.
37
Note:- You will be working with two product details.
First you need to create an empty dictionary, where you will be storing the product details.
Product Quantity= 10
Task-8 Delete release year of both the products from the inventory
Set
a set is a built-in data type that represents an unordered collection of unique elements. Sets are
similar to lists or tuples but differ in that they do not allow duplicate elements. Sets are
38
implemented using a hash table, which makes membership tests and adding/removing elements
very fast.
Creating set
# Adding elements
[Link]("grape")
[Link]({"pear", "kiwi"})
# Removing elements
[Link]("banana")
# Discarding an element (similar to remove, but does not raise an error if the element is not
present)
Set Operations:
Union (|): Combines two sets, returning a new set containing all unique elements from both sets.
39
Intersection (&): Returns a new set containing only the common elements between two sets.
Difference (-): Returns a new set containing elements present in the first set but not in the
second set.
Symmetric Difference (^): Returns a new set containing elements that are unique to each set.
40
len(): Returns the number of elements in the set.
conditions and branching are used to control the flow of a program based on certain conditions.
The primary construct for conditional statements is the if statement, and you can extend it with
elif and else to handle multiple conditions.
if Statement:
The if statement is used to test a condition. If the condition is true, the indented block of code
following the if statement is executed. Otherwise, the block is skipped.
if-else Statement:
The if-else statement allows you to execute one block of code if the condition is true and another
block if the condition is false.
41
if-elif-else Statement:
The elif (short for "else if") allows you to check multiple conditions in sequence. The code
associated with the first true condition is executed, and subsequent conditions are ignored.
Logical Operators:
You can use logical operators (and, or, not) to combine multiple conditions.
42
Nested if Statements:
You can nest if statements inside each other to handle more complex conditions.
In Python, you can use a ternary conditional expression for concise if-else statements.
Loops
loops are used to execute a block of code repeatedly. There are two main types of loops: for
loops and while loops.
for Loop:
43
A for loop is used to iterate over a sequence (such as a list, tuple, string, or range) and execute a
block of code for each element in the sequence.
Note: Range can take two argument example for I in range(10, 15). This will print out
result 10, 11, 12, 13, 14.
Using enumerate:
If you need both the index and the value, you can use the enumerate function.
while Loop:
A while loop is used to repeatedly execute a block of code as long as a specified condition is
true.
44
Copying list values to a new list
NewList=[]
i=0
[Link](squares[i])
i=i+1
Continue statement
The continue statement is used to skip the rest of the code inside a loop for the current iteration
and move to the next iteration.
45
else Clause in Loops:
Python allows an else clause to be associated with a for or while loop. The code in the else block
is executed when the loop condition becomes false.
Infinite Loops:
Be cautious with while loops to avoid infinite loops. Make sure the loop condition has a way to
become false.
Function
A function is a reusable block of code that performs a specific task. Functions are a fundamental
building block in programming, promoting code modularity, reusability, and maintainability. a
function is a reusable block of code that performs a specific task. Functions are a fundamental
building block in programming, promoting code modularity, reusability, and maintainability.
Function Definition:
46
In Python, you define a function using the def keyword, followed by the function name and a
pair of parentheses. The code block that belongs to the function is indented.
Function Invocation:
Once a function is defined, you can call or invoke it to execute the code inside the function.
Function Parameters:
Functions can accept parameters (inputs) to make them more versatile. Parameters are specified
within the parentheses during function definition.
Default Parameters:
You can provide default values for function parameters. If a value is not passed when calling the
function, the default value is used.
47
Return Statement:
Functions can return values using the return statement. The returned value can be used in the
calling code.
48
Scope of Variables:
Variables defined inside a function have local scope and are not accessible outside the function.
Variables defined outside functions have global scope. Note local variable can have same name
as a global variable without conflict. Also a variable writing in the local scope can be converted
to a global variable by adding the keyword global before the variable name.
Lambda Functions:
Lambda functions, also known as anonymous functions, are concise functions defined using the
lambda keyword. They are often used for short-lived operations.
Docstrings:
It's good practice to include a docstring to describe the purpose and usage of a function. Note you
can use the help() function to display Docstring.
49
Default functions
len()
lum()
sorted()
reverse()
def noWork():
pass
print(noWork())
You can use loops within functions to perform repetitive tasks. Below are some examples of how
you can incorporate loops into functions in Python.
50
Example 2: Looping Through a List in a Function
51
Example 4: Using a While Loop in a Function
52
Using * within a function
Exception handling
Exception handling in Python is a mechanism to deal with runtime errors, allowing you to handle
unexpected situations in a controlled manner. Python provides the try, except, else, and finally
blocks to implement exception handling.
53
Handling Multiple Exceptions:
else Block:
finally Block:
The finally block is always executed, whether an exception occurs or not. It is often used for
cleanup operations.
54
Raising Exceptions:
Classes:
Class Definition: A class is a blueprint or template for creating objects. It defines a set of
attributes and methods that the objects created from the class will have. Classes are defined using
the class keyword.
55
Attributes:
Attributes are variables that store data. They represent the characteristics or properties of an
object.
In the example above, name and age are attributes of the Dog class.
Methods:
Methods are functions that operate on the attributes of an object. They represent the behavior of
the object. In the example above, bark is a method of the Dog class.
Objects:
Object Instantiation: An object is an instance of a class. You create objects based on the class
definition. Object instantiation is done by calling the class as if it were a function.
You can access the attributes and call methods of an object using the dot notation
([Link] or [Link]()).
56
Constructor (__init__ Method):
The __init__ method is a special method called the constructor. It is automatically called when
an object is created from the class. It initializes the attributes of the object. The self parameter
refers to the newly created instance of the class.
Inheritance:
Inheritance is a mechanism where a new class inherits properties and behaviors from an existing
class. The new class is called a subclass or derived class, and the existing class is the superclass
or base class.
The Puppy class inherits from the Dog class and has an additional method play.
Encapsulation:
Encapsulation is the bundling of data and methods that operate on the data within a single unit
(class). It restricts direct access to some of an object's components, providing a way to control
the access to the data.
Example:
class Car:
[Link] = make
57
[Link] = model
[Link] = year
[Link] = 0
def accelerate(self):
[Link] += 5
def brake(self):
if [Link] > 0:
[Link] -= 5
else:
my_car.accelerate()
my_car.brake()
This is a simple example of a Car class with attributes (make, model, year, speed) and methods
(accelerate, brake). The my_car object is created, and its attributes and methods are accessed.
58
Locate and open your terminal
Type mkdir newFiles on the terminal
You can read and write files using built-in functions provided by the python language. Here's a
basic overview of file operations:
Reading a File:
To read the contents of a file, you can use the open function to open the file and then use
methods like read, readline, or readlines to read its content.
59
Writing to a File:
To write to a file, you can use the open function with the file mode set to 'w' (write). If the file
doesn't exist, it will be created. If it already exists, its content will be overwritten.
Appending to a File:
To append content to an existing file, you can use the 'a' (append) mode.
60
Exception handling when opening and closing files.
Pandas
Pandas is a popular Python library for data manipulation and analysis. It provides data structures
for efficiently storing large datasets and tools for working with them. The two primary data
structures in pandas are:
Series: A one-dimensional array that can hold any data type. It is similar to a column in a
spreadsheet or a column in a SQL table.
DataFrame: A two-dimensional table with rows and columns. It is similar to a spreadsheet
or a SQL table.
Installing pandas:
61
Importing pandas
Creating a series
Creating a DataFrame
62
pandas supports various file formats, such as CSV, Excel, SQL, and more. For example, reading
a CSV file:
A CSV (Comma-Separated Values) file is a plain text file format used to store tabular data, such
as a spreadsheet or a database. In a CSV file, each line of the file represents a row of data, and
the values in each row are separated by commas (or other delimiters, like semicolons or tabs).
Here's a simple example of what a CSV file might look like:
In this example, the first row typically contains headers, indicating the names of the columns. To
create a CSV file in Python, you can use the csv module, which provides functionality for both
reading from and writing to CSV files. eHere's an example of how you can create a CSV file and
write data to it using Python:
In this example: The [Link] object is used to write data to the CSV file. writerows is used to
write multiple rows at once. After running this code, you will find a file named
"[Link]" in your working directory containing the specified data.
63
Reading CSV file using pandas
Basic operation
Data Manipulation:
64
Handling missing data
Note you can also us [Link][0,0] to access the first-row and first column in the dataframe df?
You can also determine unique values from a dataframe using the following
df[“column_name”].unique()
65
We can also decide to fetch records that are greater than a certain period base on a column
df1=df[df[“column_name]>=10]
The new created dataframe can be saved using the following df.to_csv(“new_song.csv)
# Install ipywidgets
# Install qtconsole
Jupyter –version
66
How to create a new jupyter notebook
Click on new
Select notebook
Make sure python3 is selected then click select
Now write your code and click on the run button
To save click on file and select save notebook
Numpy
Numpy is a library for scientific computing. It has many useful functions. There are many other
advantages like speed and memory. Numpy is also the basis for pandas.
To use NumPy, you need to install NumPy before you can import and use it in your Python
script. NumPy is not included in the standard Python library, so you need to use a package
manager like pip to install it. You can install NumPy using the following command in your
terminal or command prompt:
A NumPy 1-dimensional array, often referred to as a "numpy array" or simply a "1D array," is a
fundamental data structure provided by the NumPy library in Python. It represents a one-
dimensional, homogeneous array of elements, all of the same data type. NumPy arrays are more
67
efficient for numerical operations compared to Python lists because they are implemented in C
and have a fixed size at creation.
Note ndim and shape will be suitable in a high dimension not 1 dim
68
Slicing
a. Vector addition
Adding constant to a numpy array if we add a scalar to the array, numpy will add it to every
element of the array this property is known as broadcasting
b. Vector subtraction
69
c. Array multiplication with scalar
e. Dot product
70
Universal function (Sum, mean, max, min)
71
We can use numpy to create functions that map numpy to new numpy arrays
We can access the value of pie in numpy as follows. We can create the following numpy array in
radians. This array corresponds to the following vector. We can apply the function sin to the
array x and assign the values to the array y. This applies the sin function to each element in the
array, this corresponds to applying the sine function to each component of the vector. The result
is a new array y, where each value corresponds to a sine function being applied to each element
in the array x.
Linspace function
The linspace function is part of the NumPy library, and it is used to create evenly spaced values
over a specified range. The syntax for linspace is as follows:
72
start: The starting value of the sequence.
stop: The end value of the sequence.
num: The number of evenly spaced values to generate. (Default is 50)
endpoint: If True, stop is the last value in the range. If False, the range doesn't include
stop. (Default is True)
retstep: If True, return the step size between values. (Default is False)
dtype: The data type of the output array. If not specified, the data type is inferred from the
input values.
axis: The axis in the result along which the linspace samples are stored. The default is 0.
Example
To plot graph we need to first use pip and install a dependence called matplotlib as follow
We can use the function line space to generate 100 evenly spaced samples from the interval zero
to two pie. We can use the numpy function sin to map the array x to a new array y. We can
import the library pyplot as plt to help us plot the function. As we are using a Jupiter notebook,
we use the command matplotlib inline to display the plot. The following command plots a graph.
The first input corresponds to the values for the horizontal or x-axis. The second input
corresponds to the values for the vertical or y-axis.
73
Two dimensional numpy
We can create numpy arrays with more than one dimension. This section will focus only on 2D
arrays but you can use numpy to build arrays of much higher dimensions. Note he term "2D" in
the context of a 2-dimensional array does not imply a specific number of rows or columns
example 2 rows and 2 columns. Instead, it indicates that the data structure has two dimensions,
typically organized in rows and columns.
74
Using various attribute on the array
Using slicing
75
Example 2
Addition
Example
76
Multiplication by scalar
Example
77
Multiplication
Example
78
Multiplication
We can also perform matrix multiplication with Numpy arrays. Matrix multiplication is a little
more complex but let's provide a basic overview. Consider the matrix A where each row is a
different color. Also, consider the matrix B where each column is a different color. In linear
algebra, before we multiply matrix A by matrix B, we must make sure that the number of
columns in matrix A in this case three is equal to the number of rows in matrix B, in this case
three.
79
From matrix multiplication, to obtain the ith row and jth column of the new matrix, we take the
dot product of the ith row of a with the jth columns of B. For the first column, first row we take
the dot product of the first row of A with the first column of B as follows. The result is zero. For
the first row and the second column of the new matrix, we take the dot product of the first row of
the matrix A, but this time we use the second column of matrix B, the result is two. For the
second row and the first column of the new matrix, we take the dot product of the second row of
the matrix A. With the first column of matrix B, the result is zero. Finally, for the second row and
the second column of the new matrix, we take the dot product of the second row of the matrix A
with the second column of matrix B, the result is two. In numpy, we can define the numpy arrays
A and B.
Example
80
Simple API
In this video we will discuss Application Program Interfaces API for short. Specifically, we will
discuss: What is an API API Libraries REST API, including: Request and Response An Example
with PyCoinGecko An API lets two pieces of software talk to each other For example you have
your program, you have some data, you have other software components. You use the api to
communicate with the api via inputs and outputs. Just like a function, you don’t have to know
how the API works, but just its inputs and outputs. Pandas is actually a set of software
components, much of which are not even written in Python. You have some data. You have a set
of software components. We use the pandas api to process the data by communicating with the
other Software Components. Let’s clean up the diagram. When you create a dictionary, and then
create a pandas object with the Dataframe constructor, in API lingo, this is an “instance.” The
data in the dictionary is passed along to the pandas API. You then use the dataframe to
communicate with the API. When you call the method head, the dataframe communicates with
the API displaying the first few rows of the dataframe. When you call the method mean the API
will calculate the mean and return the values.
REST APIs are another popular type of API; they allow you to communicate through the internet
allowing you to take advantage of resources like storage, access more data, artificial intelligent
algorithms, and much more. The RE stands for Representational, the S stands for State, the T
81
stand for Transfer. In rest API’s your program is called the client. The API communicates with a
web service you call through the internet. There is a set of rules regarding Communication, Input
or Request, and Output or Response. Here are some common terms. You or your code can be
thought of as a client. The web service is referred to as a resource. The client finds the service via
an endpoint. We will review this more in the next section. The client sends requests to the
resource and the the resource (web service) sends a response to the client. HTTP methods are a
way of transmitting data over the internet We tell the Rest API’s what to do by sending a request.
The request is usually communicated via an HTTP message. The HTTP message usually
contains a JSON file. This contains instructions for what operation we would like the service to
perform. This operation is transmitted to the webservice via the internet. The service performs
the operation. In the similar manner, the webservice returns a response via an HTTP message,
where the information is usually returned via a JSON file. This information is transmitted back to
the client. Crypto Currency data is excellent to be used in an API because it is being constantly
updated and it is vital to CryptoCurrency Trading We will use the Py-Coin-Gecko Python
Client/Wrapper for the Coin Gecko API, updated every minute by Coin-Gecko We use the
Wrapper/Client because it is easy to use so you can focus on the task of collecting data, we will
also introduce pandas time series functions for dealing with time series data Using Py-Coin-
Gecko to collect data is quite simple All we need is to install and import the library Create a
client object And finally use a function to request our data. In this function we are getting data on
bitcoin, in U.S. Dollars, for the past 30 days. In this case our response is a JSON expressed as a
python dictionary of nested lists including price, market cap, and total volumes which contain the
unix timestamp and the price at that time. We are only interested in price so that is what we will
select using the key price To make things simple, we can convert our nested list to a DataFrame,
with the columns time stamp and price its difficult to understand the column time stamp we Will
convert it to a more readable format using the pandas Function to_datetime
Using the to datetime function, we create readable time data, the input is the time stamp column
unit of time is set to milliseconds We append the output to the new column date
82
We. Would like to create a candle stick plot To get the data for the daily candlesticks we will
group by the date to find the minimum, maximum, first, and last price of each day Finally we
will use plotly to create the candlestick chart and plot it Now we can view the candlestick chart
by opening the html file and clicking trust HTML in the top left of the tab It should look
something like this
Example
Fetching historical market data for Bitcoin using the CoinGecko API and then create a
DataFrame with the timestamp and price information.
83
To make the time stamp readable we convert it to datetime
Candlestick plot
84
A candlestick plot, or candlestick chart, is a type of financial chart used to represent the price
movement of an asset, such as a stock, currency, or commodity, over a certain time period. It is
commonly used in technical analysis of financial markets. The chart consists of individual
"candlesticks" that visually represent price movements.
85
Anapralin
86