Python Programming Basics for Finance
Python Programming Basics for Finance
FROM PERU
January, 2020
PROGRAMMING IN PYTHON
Basic Level
Index
About of Python 6
1.1. What Is it Python? . . . . . . . . . . . . . . . . . . . .6. . . . . . . . .
1.2. Characteristics from Python . . . . . . . . . . . . . . . . . 6. . . . . . . .
1.3. Brief history of Python. . . . . . . . . . . . . . . . . .7. . . . . . . .
Identifiers of Python 24
3.1. Rules to write identifiers24
3.2. Some considerations. . . . . . . . . . . . . . . . . . . . . . . . . . 24
4. Operators 25
4.1. What Who are the operators? 25
4.2. Operadores arithmetic25
4.3. Operators of comparison (relational)26
4.4. Operators logical (boolean) 27
4.5. Operators of Assignment 27
4.6. Operators Specials...
4.6.1. Operator of identity . . . . . . . . . . . . . . . . . . . . . . . 28
4.6.2. Operator of Membership 28
5. Types of Data 30
5.1. Indexing30
5.2. Numbers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
5.3. Sequences32
5.3.1. Chains Strings...
5.3.2. List35
5.3.3. Tuple 39
5.4. Sets (Set) 40
5.5. Dictionary 41
5.6. Conversion between data types 42
8. Sentences of Control 50
8.1. Sentences selective: If, If-Else and If-Elif-Else 50
8.1.1. Sentence Selective if 50
8.1.2. Sentence Selective if...else 50
8.1.3. Sentence if...elif...else51
8.1.4. Sentences if nested 51
8.1.5. Operator ternary . . . . . . . . . . . . . . . . . . . . . . . . . . 52
8.2. Sentences iterative52
8.2.1. Judgment Iterative for 53
8.2.2. Sentence While loop. . . . . . . . . . . . . . . . . . . . . . 54
9. Functions 56
9.1. What Is it a function in Python? 56
9.2. Types of functions56
9.2.1. Function conventional56
9.2.2. How call a function? . . . . . . . . . . . . . . . . . . . 57
9.2.3. The return statement 57
9.2.4. Types of function arguments 58
9.2.5. Functions Lambda60
9.3. Docstring 60
9.4. Scope and lifespan of the variables61
9.5. Storage of its functions in Modules 62
[Link] 65
11.1. What are packages? . . . . . . . . . . . . . . . . . . . . . . . . . . 65
11.2. The Scientific Stack . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
11.3. What are modules in Python?. . . . . . . . . . . . . . . . . . . . . 67
11.4. How to import modules in Python? ............... 67
11.5. Import judgment68
11.6. Import with renaming68
11.7. Judgment from ... import ... . . . . . . . . . . . . . . . . . . . . . . . . 69
11.8. Import all functions69
11.9. Python module search 70
[Link] a module70
[Link] internal functiondir()71
13. Pandas 84
13.1. What is Pandas?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
13.2. Series84
13.2.1. Creation and indexing of Series 85
13.2.2. Size, shape, and value count 88
13.2.3. Alignment using index labels89
13.3. The DataFrame 90
13.3.1. Creation of a DataFrame 90
13.3.2. Selection of columns from a DataFrame 93
13.3.3. Selection of rows from aDataFramethrough the index ...
13.3.4. Selection of lines using Boolean selection. . . . . . . . . 97
13.3.5. Arithmetic in a DataFrame 98
13.4. Reindexing of objectsSeriesyDataFrame99
13.5. DataFrame Manipulation Applied to Finance.103
13.5.1. Data reorganization and restructuring 104
13.5.2. Grouping and aggregation115
[Link] 122
14.1. Introduction ...
14.2. Pyplot 123
14.3. Multiple figures 124
14.4. Elements of the graphic . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
14.4.1. Text126
14.4.2. Grid 129
14.4.3. Legend 130
14.5. Types of graphs132
14.5.1. Line graphs 132
14.5.2. Line graphs with pandas 134
14.5.3. Histograms 135
14.5.4. Bar charts . . . . . . . . . . . . . . . . . . . . . . . . . 136
14.5.5. Horizontal bar charts 138
14.5.6. Multiseries bar graphs with pandas DataFrame ... 140
14.5.7. Stacked multi-series bar charts ...
14.5.8. Stacked bar graphs with a pandas DataFrame. . . . . 142
14.5.9. Other Representations of Bar Graphs143
[Link] charts144
[Link] charts with a pandas DataFrame ....... 147
[Link] graphics: Polars 147
14.6. 3D Graphics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149
14.6.1. 3D Surface 149
14.6.2. 3D bar charts150
14.7. Multi-Panel Graphics 151
14.7.1. Visualize subplots within other subplots151
1. About Python
1.1. What is Python?
Python is a programming language-
multiplatform action (that is, it acts-
cuts on multiple platforms such as
Windows, Mac OS X, Linux, Unix and
it has even adapted to the machi-
Java and .NET virtual machines.
1. Easy: The code, in Python, is concise and effective and therefore understandable.
manageable.
6. Type of objects generated: Python has built object types. This makes
that the task to be done is easy and manageable.
Numerous libraries and tools: The task to be performed becomes easy, given
due to the fact that most common tasks (in fact, not so common ones too)
they have already been programmed in Python.
Web pages
Database programming
Gambling
Component-based programming
In 2001, he received the FSF Award for the Advancement of Free Software for having invented and implemented
the Python programming language. In December 2005, he was hired as a developer by the
American company Google, and after seven years, at the beginning of December 2019, announces its
withdrawal from the American company to join Dropbox in January 2013.
3 Thecontinuous central role of Guido van Rossum in the leadership of Python is reflected in the title.
that the Python community gave him, "Benevolent Dictator For Life" (BDFL, for its initials in Spanish)
English).
Python
applications
Numpy, Pandas
Django Kiwi Scions Scipy, Matplotlib [Link]
statsmodels pandas_datareader
yahoo-finance
Python finance
finance,economics.
Many development environments and editors are available for Python. Some
the following are from them:
Spyder
2. Jupyter Notebook
3. Jupyter Lab
5. Emacs
6. Vim
7. Gedit
8. Idle
Step 1: go to[Link]
-Select 'Just Me' if you only want it to be installed for your Windows user.
Click on 'Install'.
Click on Next.
Click on 'Finish'.
Immediate mode
Python 3.7.4 (latest version as of the date) you can download it from the link:
[Link]
The use of an IDE can eliminate redundant tasks and significantly reduce
the time required for application development.
The IDE is a piece of software that provides useful functions such as its-
code management, syntax highlighting and checking, explorers of
files, etc. to facilitate the programmer's development of applications.
An IDE is used to run a Python program written in a file.
That file is called unscript.
Losscripts can be saved to disk for future use. It is important.
It should be mentioned that to write a Python script file, one can use any.
which text editing software. It should only be saved with the extension
.py.
The following are three IDEs (free) that will be explored in the course:
∗ The IPython panel (IPython Pane) is the main way that in-
You interact with Python. It can be used to run programs of
Python computer, test Python code snippets, navigate through
the computer's file directories and perform system tasks
how to create, move, and delete files and directories. The prompt of
the default input of the IPython panel looks like this:
In [1]:
This notice means that Spyder is running the IPython shell.
∗ The Editor Pane allows you to write and edit programs
more Python (or scripts), which are simply sequences of commands
two Python (code) stored in a file on your computer.
∗ The Help Pane in Spyder provides assistance with the commands.
two of Python. An interesting feature of the Spyder IDE is
that if you place the cursor next to or inside the name of a function
and press Ctrl+I, the web documentation of the function is displayed
in the Help Panel. Another way to obtain information from a
function, is executing the help command, followed by the name of the
function in parentheses, for example:
In [1]:help(range)
Step 4. Click the run button in the Spyder IDE. It will be visualized.
he curses the following output.
In the lower left corner of the control panel, there is a list of folders
(and files) from the default directory. You can navigate to the folder
Where do you want to save your work, or create a new folder, by doing
click on the New button located at the top right of the panel and
selecting Folder. Finally, to create a new Jupyter notebook
Go to the New dropdown menu on the right side of the page and
select Python 3.
Jupyter Notebook
The video will be displayed below the code, as shown in the figure.
previous
c) JupyterLab mode
JupyterLab is a web-based interactive development environment for
code and data Jupyter. While JupyterLab allows the use of notebooks
Jupyter goes beyond the classic Jupyter Notebook by providing a
flexible and extensible web application with a set of components
reusable.
∗ JupyterLab is flexible: the interface must be configured and organized
user to support a wide range of workflows in science
data, scientific computing, and Matching Learning.
∗ JupyterLab is extensible and modular: write plugins that add new
your components and integrate with the existing ones.
To set your computer in the default home directory, use the following
code:
In [1]: %cd ~
/Users/Yessenia
To show the current directory path of the computer, you can use the code:
In [2]: %pwd
/Users/Yessenia
To create a folder inside your documents directory, type the code %ls
to list the files and directories in your root directory, then write %mkdir
programs and this new folder will be created in your directory. Finally, to validate
a new folder has been created, run %ls.
In [7]: %cd ~
/Users/pine
In [8]: %ls
Applications/ Library/ Pictures/
Desktop/ Movies/ Public
Documents/ Music
Downloads/ News/
In [10]: %ls
Applications/ Library/ Pictures/
Desktop/ Movies/ Public/
Documents/ Music programs/
Downloads/ News/
To clear the IPython terminal, type the command %clear or cls, which will give you
a new interpreter window.
2.4. Pyflakes
One way to avoid errors is to use a syntax checker. Fortunately,
The Spyder IDE includes a syntax checker called Pyflakes that
executes in the background when editing a Python program using
Spyder. If there is an error in your code, Pyflakes marks the error.
The Spyder Editor provides syntax highlighting, which encodes keywords, co-
comments and other features of Python syntax according to its function, and
therefore it facilitates code reading and the detection of programming errors.
The spell checker also provides syntax checking, such as a
spell checker in a word processing program that identifies
many coding errors. This can greatly speed up the process of
coding.
In Spyder, a red circle appears to the left of the line where Pyflakes thinks
that the error occurs. A yellow triangle appears to the left of the line where
Pyflakes thinks that the coding style does not conform to PEP 8 standards.4 this
It is not an error, just a violation of coding style, which you can ignore.
or to take into account. When hovering the mouse pointer over the red or yellow icon,
a code analysis box appears with a brief message describing the error
or the style infringement.
4 It
is a style guide document for Python, which is not mandatory to follow, but is highly recommended.
recommended. You can find more information at:[Link]
3. Python Identifiers
An identifier is the name assigned to entities such as classes, functions, variables,
etc; helping to differentiate one entity from another.
(_).
Names like 'miClase', 'var_1', 'yy' print on screen, are valid examples.
two.
2. An identifier cannot start with a digit. 1variableno is not valid, but
variable1 is perfectly fine.
4. Operators
4.1. What are operators?
Operators are special symbols in Python that perform arithmetic calculations.
or logicals.
The value at which the operator operates is called operand.
For example:
>>> 2+3
5
Here, + is the operator that performs the addition, 2 and 3 are the operands and 5 is the output.
of the operation.
# Output dif:
x - y # Output Potencia:
x raised to the power of y
Output mult:
x > y is False
x is greater than y
x < y is True
x is less than y
x == y is False
x==y
x != y is True
x is not equal to y
x >= y is False
x is greater than or equal to y
x <= y is True
x is less than or equal to y
In many cases, the execution of a block depends on the true value of more than one
sentence. In these cases, the operators "and" ("&") and "or" ("|") are used
in our rescue. The first ('and') is used when the output is 'true',
when both conditions are 'true'. The second ('or') is used if the output
it is 'true' if any of the conditions is 'true'.
Example: Logical Operators
x = True
y = False
x and y is False
xandy
x or y is True
xory
not x is False
notx
# Output: False
x1 is not y1
# Output: True
x2isy2
# Output: False
x3isy3
∗ It is visualized that x1yy1 are integer numbers of the same values, for the
they are equal and identical. The same happens with x2yy2 (strings).
∗ However, they are lists. They are the same but not identical. It's because the
interprets them separately in memory even though they are the same.
In a dictionary, we can only test for the presence of a key, not the value.
# Output: True
Hinx
# Output: True
hellonot inx
# Output: True
1iny
# Output: False
'a'tiny
5. Data Types
Python allows the user to manipulate variables to store values, and each value
In Python, there is a data type (they do not need to be declared).
Objects in Python have identity, a type, and a value (given by the user)
or a default value). Identity in Python refers to the address and not
change. The data type can be any of the following.
a) Strings
b) Tuples
c) Lists (list)
a) Sets
b) Dictionaries
5.1. Indexing
In Python, the index operator [] allows you to access an element of a
sequence.
5.2. Numbers
The numeric category is composed of integers, the numbers of
floating point and complex numbers. The classes int, float, and are defined.
complex in Python.
The function type() allows you to check what type of data a variable belongs to.
or a value; and the isinstance() function to check if an object belongs to a
particular data type.
a = 5
type(a)
b = 2.0
type(b)
c = 1 + 2j
isinstance(c, complex)
Python calls any number with a decimal point: a floating point number
aunt. This term is used in most programming languages, and
It refers to the fact that a decimal point can appear in any position.
of a number. This type of data is accurate up to 15 decimal places.
Complex numbers are written in the form,x+ yj wherexit is the real part
ey it is the imaginary part.
Examples.
a = 1234567890123456789
a
1234567890123456789
b = 0.1234567890123456789
b
0.12345678901234568
c = 1+2j
c
(1+2j)
5.3. Sequences
5.3.1. Strings
In Python, a string is a predefined object that contains a series of characters.
teres.
This is a string.
This is also a string.
This flexibility allows the use of quotes and apostrophes within strings:
I can say, 'Python is my favorite language!'
The language 'Python' is named after Monty Python.
One of the strengths of Python is its diverse and supportive community.
The value of this string can be displayed simply by writing the name of the
object in the command line. or using the print() function.
>>>name Harsh
print(name)
Concatenate String: To concatenate strings, Python uses the plus sign +. And the
operator*, concatenates a string given the number of times. For example, 3*name
I would say "harsharshharsharsharsharsh." For example:
name = name + 'arsh'
print(name)
Harsharsh
3*name
HarshHarshHarsh
Python can look for additional whitespace on the right and left sides
from a string. To ensure that there are no white spaces in the
right end of a string, use the rstrip() method.
>>> favorite_language =’python ’
>>> favorite_language
python
>>> favorite_language.rstrip()
python
>>> favorite_language
python
You can also remove the white spaces from the left side of a string
using the lstrip() method or removing whitespace from both sides of the
you can use strip():
>>> favorite_language =’ python ’
>>> favorite_language.rstrip()
python
>>> favorite_language.lstrip()
python
favorite_language.strip()
python
For example, if we want to know the first character of the string, we should write
the following code:
print(name[0])
H
Cutting a string: it refers to removing some part of the string. For example:
>>>name =’Sonam’
>>>name
Sonam
To extract the part after the first letter, or after the first two.
letters we can write:
>>> name1=name[1:]
>>> name1
Onam
>>>name = name[2:]
>>>name
I
The string in Python is not mutable; that is, once defined, the value of a
the string cannot be changed. Just like with the list and the tuple, the slicing operator
It can be used with a string. Strings are immutable.
Hello world!
s[4] = 'o'
print(s[4] = , s[4])
world
print(s[6:11] =
Error generation
Strings are immutable in Python
s[5] =’d’
Avoiding Type Errors with the str() Function: Python knows that the variable
it could represent the numerical value 23 or the characters 2 and 3. When you use nume-
integer values within strings like this, you need to specify explicitly that
you want Python to use the integer as a string of characters. It can.
do this by wrapping the variable in the str() function, which tells Python that
represente valores que no sean strings como strings. Ejemplo:
age = 23
message ="Happy "+str(age) +"rd Birthday!"
print(message)
5.3.2. List
A list is an ordered sequence of elements. It is one of the data types.
most used in Python and it is very flexible. It is not necessary for all the elements
that a list contains elements of the same type.
For example.
a = [5,10,15,20,25,30,35,40]
a[2] = 15
print(a[2] = , a[2]
Lists are mutable, that is, the value of the elements in a list can
to change oneself.
a = [1,2,3]
a[2]=4
>>> a
[1, 2, 4]
motorcycles[0] =’ducati’
print(motorcycles)
The output shows that the first element has been modified and that the rest of the
the list remains the same:
[’honda’,’yamaha’,’suzuki’]
[’ducati’,’yamaha’,’suzuki’]
You can modify the value of any element in a list, not just the first one.
element.
[Link]('ducati')
print(motorcycles)
[’honda’,’yamaha’,’suzuki’,’ducati’]
If you need to add a new element at any position in the list using
the function insert(). This is done by specifying the index of the new element and the
value of the new position. Example:
motorcycles = [’honda’,’yamaha’,’suzuki’]
[Link](0,'ducati')
print(motorcycles)
[’ducati’,’honda’,’yamaha’,’suzuki’]
Remove elements from a list: you can use the del function, if you know the
position of the element you want to remove from a list. Example:
motorcycles = [’honda’,’yamaha’,’suzuki’]
print(motorcycles)
delmotorcycles[0]
print(motorcycles)
If you want to remove the last element from the list, you can use the pop() function.
which also allows you to work with that element after deleting it.
motorcycles = [’honda’,’yamaha’,’suzuki’]
popped_motorcycle = [Link]()
print(motorcycles)
print(popped_motorcycle)
The output shows that the value of the last element was removed from the end of the
list and is now stored in the variable popped_motorcycle.
[’honda’,’yamaha’]
suzuki
You can use pop() to remove an element from a list at any position.
including in parentheses the index of the element you want to delete.
motorcycles = [’honda’,’yamaha’,’suzuki’]
first_owned = [Link](0)
If you do not know the position of the value you want to remove from a list, you can use
the method remove(). This function removes only the first occurrence of the value
specified.
motorcycles = [’honda’,’yamaha’,’suzuki’,’ducati’]
too_expensive =’ducati’
[Link](too_expensive)
Sorting a list
A list can be sorted temporarily using the sort() method. For example:
organize alphabetically.
cars = [’bmw’,’audi’,’toyota’,’subaru’]
[Link]()
print(cars)
[’audi’,’bmw’,’subaru’,’toyota’]
This function can also sort in reverse, using the argument reverse =
Trueal methodsort(). For example:
cars = [’bmw’,’audi’,’toyota’,’subaru’]
[Link](reverse=True)
print(cars)
[’toyota’,’subaru’,’bmw’,’audi’]
Once the function is executed, the order of the list changes permanently. Without
embargo, if a temporal order is required, you can use the sorted() function.
example:
cars = [’bmw’,’audi’,’toyota’,’subaru’]
print(Here is the original list:
print(cars)
Here is the original list:
["bmw","audi","toyota","subaru"]
print(Here is the sorted list:
print(sorted(cars))
Here is the sorted list:
[’audi’,’bmw’,’subaru’,’toyota’]
[Link]
You untuple a sorted sequence of elements like in a list. The only
the difference is that tuples are immutable, that is, once created they cannot
be modified.
Tuples are used to protect data from being written and are usually more
faster than lists, since they cannot be changed dynamically.
tup3
(1, 2, 3, 4)
For example:
num1= 2
num2= 3
Output
Such constructions can be useful for making tables and other structures.
you can access the different elements of a list with a direct extension of the
indexing scheme that we have been using.
Examples:
5.4. Sets
A set is an unordered collection of unique items. A set is
defined by comma-separated values within braces {}. The items in a
set not organized.
a = {5,2,3,1,4}
5.5. Diccionario
A dictionary is an unordered collection of key-value pairs.
The key is used to retrieve the respective value. But not the other way around.
print(d[1] = , d[1])
d[1] = value
A key-value pair is a set of values associated with each other. Each value is
connected to its value by two points, and the individual key-value pairs are
separated by commas.
The conversion defloat will truncate the value (bring it closer to zero).
10
10
>>>int(-10.6)
-10
The conversion to and from the string must contain compatible values.
>>>float('2.5')
2.5
25
25
where we prompt the string that we want to display on the screen. When it is executed
the input function prints on the computer screen the text in the quotes
and waits for user input. The user types a string.
num = input('Enter a number: ')
Enter a number: 10
num
10
float(’10’
10.0
where:
Example:
print(’This sentence is output to the screen’)
This sentence is output to the screen
a = 5
print(The value of a is
The value of a is 5
print(1,2,3,4,sep=’*’)
# Output: 1*2*3*4
print(1,2,3,4,sep=#&
1#2#3#4&
We can even format strings like the old printf() style used in
the programming language C. We use the % operator to achieve this.
x = 12.3456789
print(The value of x is %3.2f
The value of x is 12.35
Where,
∗ filename, indicates the name of the data file to be created, which must be
create in quotes. If the file name already exists, it will be overwritten.
∗ array, indicate the list of column names of the data to be exported,
use the zip function if you want to package the columns into one.
∗ fmt is an optional argument where you can configure the number of
decimal digits in exponential notation (e) or in float notation (f).
∗ delimiter, by default a blank space is left when it comes to
txt files. and a comma must be configured when it comes to csv files.
∗ header is a string variable that allows you to write the header text
about the data.
likewise
colors = [’red’,
blue
green
7.2. Indentation
In programming, indentation means moving a block of text to the right.
by inserting spaces or tabs, in order to separate it from the left margin and
distinguish it better from the adjacent text; in the field of printing, this concept
It has always been called bleeding or indentation.
A block of code (the body of a function, loop, etc.) begins with an indentation
and ends with the first line flush left. The amount of indentation depends on the
programador, sin embargo, debe ser consistente en todo el bloque.
In general, 4 spaces are used for indentation and tabs are preferred.
Here is an example.
for i in range(1, 11):
print(i)
ifi == 5:
break
The use of indentation makes the code look clean and organized. This provides
as a result, programs that look similar and consistent.
y
ifTrue:print(Hello); a = 5
Both are valid and do the same thing; however, the first style is clearer.
In Python, the hash symbol (#) is used to start writing a comment, which
write after a hash mark in your code, it is ignored by the interpreter
Python.
Example:
This is a comment
Print Hello
print(Hello
These triple quotes are generally used for multi-line strings, like this
as for multi-line comments. Unless they are docstrings, no
they do not generate any additional code.
This is also a
perfect example of
multi-line comments
7.3.2. Docstring
Docstring is the abbreviation for documentation string.
8. Control Statements
8.1. Selective statements: If, If-Else, and If-Elif-Else
Selective statements are an integral part of programming, for decision making.
of decisions. Python recognizes a block of code through indentation.
Indentation determines the beginning and the end of a block in Python. Therefore, it is
It is important to be careful with indentation, otherwise an error will occur.
code output.
The if <condition> statement is followed by a colon.
There is no need for parentheses for this test condition. Although including the
The test in parentheses will not result in an error.
Example
If the number is positive, the appropriate message is printed.
num = 3
ifnum > 0:
print(num,it is a positive number.
print("Esto se imprime siempre.")
num = -1
ifnum > 0:
print(num,it is a positive number.
print(This is always printed too.
Example.
Program that checks if the number is positive or negative
And show an appropriate message
num = 3
ifnum >= 0:
print(Positive Number or Zero
else:
print(Negative Number
Syntax
if test_expression1:
sentence(s)1
else if test_expression2:
sentence(s)2
else:
sentence(s)3
Example: Find the largest of the three numbers entered by the user, using
a ternary operator.
Ternary Operator
a = int(input('Enter the first number :'))
b = int(input('Enter the second number :'))
c = int(input('Enter the third number :'))
For example
Program that finds the sum of all the numbers in a list
List of numbers
numeros = [6, 5, 3, 8, 4, 2, 5, 4, 11]
Program execution
The sum is 48
Example 1.
range(0, 10)
print(range(10))
# Output: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print(list(range(10)))
# Output: [2, 3, 4, 5, 6, 7]
print(list(range(2, 8)))
Example 2.
Program to iterate through a list using indexing
genre = [’pop’,’rock’,’jazz’]
Program execution
I like pop
I like rock
I like jazz
For example.
digits = [0, 1, 5]
forinindigits
print(i)
else:
print("No items left."
For example.
Program that adds the natural numbers from 1 to n
# sum = 1+2+3+...+n
n = int(input("Enter n: "))
n = 10
initialize sum and the counter
sum= 0
i = 1
while i <= n:
sum = sum + i
i = i + 1 update the counter
print the sum
print(The sum is sum)
Output.
Enter n: 10
The sum is 55
For example.
Example to illustrate
the use of the else statement
with the while loop
counter = 0
Output.
Inside the loop
Inside the loop
Inside the loop
Inside the else
9. Functions
9.1. What is a function in Python?
In Python, a function is a group of related statements that perform a
specific task.
Functions help to divide our program into smaller and more manageable parts.
dollars. As our program grows more and more, the functions do too.
more organized and manageable.
where:
The word 'clavedef' marks the beginning of the function header and informs
In Python, a function is being defined.
The name of the function allows it to be identified uniquely.5
3. Arguments, through which we pass values to a function and
declared in parentheses. They can be mandatory or optional.
4. The colon (:) denotes the end of the function header.
5. The documentation string (docstring) is optional to describe what
make the function. The Docstrings are declared between triple quotes and should
appear immediately after the function definition.
6. The body of the function is comprised of one or more statements. The decla-
Indentations must have the same level of indentation (usually 4 spaces).
7. The return statement (optional) allows the function to return a value.
5 The naming of functions follows the same rules for writing identifiers in Python.
The same convention should be used for keyword arguments when invoking the
function.
function_name(value_0, parameter_1='value')
Syntax
return[expression_list]
This statement may contain an expression that is evaluated and returns the value.
If there is no expression in the declaration or the return statement is not present,
the function will return the object None.
For example:
def absolute_value(num):
This function returns the value
absolute of the entered number
ifnum >= 0:
returnnum
else:
return-num
# Output: 2
print(absolute_value(2))
Output: 4
print(absolute_value(-4))
tipo_derivados(’Opciones’,’Europeas’)
tipo_derivados(tipo=’Opciones’,subtipo=’Europeas’)
Default Arguments: When writing a function, you can define a default value.
for each parameter. If a value is provided for an argument when invoking the
function, Python uses the value of the argument. If not, it uses the value by
default of the argument. The use of the proposed values can simplify
the use of the function and clarify the ways in which it is usually used
the functions. When using default values, any argument with
a default value must appear after all non-default arguments
They have default values. This allows Python to continue interpreting
correctly the positional arguments.
derived_type(subtype,type=Options
Information about financial derivatives
print(One of the types of derivative assets is:
print(It has subtypes:
tipo_derivados(subtipo=’Europeas’)
Example:
We define the name format function, where the second name is optional.
def get_formatted_name(first_name, last_name, middle_name=’’):
Return a full name, neatly formatted.
ifmiddle_name:
full_name = first_name +’ ’+ middle_name +’ ’+ last_name
else:
full_name = first_name +’ ’+ last_name
returnfull_name.title()
musician = get_formatted_name(’jimi’,’hendrix’)
print(musician)
musician = get_formatted_name(’john’,’hooker’,’lee’)
print(musician)
To make the middle name optional, the argument middle_name was assigned.
a default empty value and what causes the argument to be ignored unless the
user provide a value.
or also
def(arguments):
returnresult
Example.
f=lambda a,b,c=1: (4*a-b)/c
9.3. Docstring
The first string after the function header is called a docstring and
it is the abbreviation for documentation chain. It is used to briefly explain,
what a function does.
Although optional, documentation is a good programming practice.
less than I can remember what I ate last week, always document your
code.
In the previous example, we have a string of documents immediately below
from the header of the function.
∗ The parameters and variables defined within a function are not visible.
from the outside. Therefore, they have a local reach.
The useful life of a variable is the period during which the variable is out of the
memory.
∗ They are destroyed once we return from the function. Therefore, a function
It does not remember the value of a variable from its previous calls.
x = 20
my_func()
print(Value outside function:
Exit
Value inside the function: 10
Value outside the function: 20
∗ We can see that the value of dexes 20 initially. Although the function my_func()
the value of dexa10 changed, it did not affect the value outside of the function.
∗ This is because the variable inside the function is different (local to the
function) that is outside. Although they have the same names, they are two
different variables with a different scope.
∗ On the other hand, variables outside the function are visible from inside.
They have a global reach.
∗ We can read these values from inside the function, but we cannot
change them (write them). To modify the value of the variables outside of the
function, must be declared as global variables using the keyword
global.
Store your functions in a separate file: it allows you to hide the details of
code of your program and focus on its high-level logic. It also allows
reuse functions in many different programs.
∗ A module is a file that ends in .py that contains the code that
you want to import into your program. The following section will describe in more detail
the modules.
∗ A module is created that contains the function make_pizza(), saving it in
the file [Link]
[Link]
def make_pizza(size, *toppings):
Summarize the pizza we are about to make.
print(Making a str(size) +
-inch pizza with the following toppings:
fortoppingintoppings
print(-
main_program.py
importpizza
pizza.make_pizza(16,’pepperoni’)
pizza.make_pizza(12,’mushrooms’,’green peppers’,’extra cheese’)
One can perceive a class as a prototype, which has attributes and behavior.
treatment. Attributes generally store data and behavior is
implement through functions.
The attributes represent the characteristics of the entity to which they pertain.
Movie
Class
The fault in
Love actually Sarat
our stars
Objects
movie
name
Name year
genre
director
producer Class
Attributes actors
music director
story writer
getdata()
Behaviour putdata()
def __setup_parameters__(self):
Required calculations for the model
self.M = self.N + 1 # Number of terminal nodes of tree
self.u = 1 + [Link]# Expected value in the up state
self.d = 1 - [Link] # Expected value in the down state
[Link] = ([Link](([Link])*[Link]) -
self.d) / (self.u-self.d)
[Link] = 1 - [Link]
def_initialize_stock_price_tree_(self):
Initialize terminal price nodes to zeros
[Link] = [Link](self.M)
def_initialize_payoffs_tree_(self):
Get payoffs when the option expires at terminal nodes
payoffs = [Link](
0, ([Link]-self.K) if self.is_call
else([Link])
return payoffs
11. Packages
11.1. What are packets?
We usually do not store all our files on our computer.
the same location. We use a well-organized directory hierarchy to
facilitate access.
Similar files are saved in the same directory, for example, we can
keep all songs in the 'music' directory. Just like this, Python
packages are to directories and modules are to files.
We can define our most used functions in a module and import it, in
place to copy your definitions into different programs.
Let's see another example of how to create a module. We write the following and save it.
we give as [Link].
Python Modulo example
defadd(a, b):
This program adds two numbers and returns the result.
result = a + b
returnresult
Using the module name, we can access the function using the operation
from point (.). For example:
>>> [Link](4,5.5)
9.5
∗ You can consult the complete list of standard Python modules and for
what they are for. These files are located in the directory Lib within the
location where Python was installed.
There are several ways to import modules. The ones described below:
import math
print(The value of pi is
import math as m
print(The value of pi is
We have changed the name of the module mathporm. This can save us time.
of writing in some cases.
Please note that the name mathno is not recognized within our scope;
both, [Link] invalid, [Link] the correct implementation.
You can also import as many functions as you want from a module, separating them
name of each function with a comma. The syntax is:
from module_name import function_0, function_1, function_2
With this syntax, it is not necessary to use dot notation when calling
to a function.
Example.
importing only pi from the math module
How we only import the attribute of the module. We do not use the dot operator.
e
2.718281828459045
The asterisk in the import statement tells Python to copy each function
from the module to the current program file. And since all functions are
important, you can call each function by its name without using dot notation
points.
Example.
import all names from the standard math module
We import all the definitions from the math module. This makes all
the names, except for those that begin with an underscore, should be visible in our
scope.
Importing everything with the asterisk (*) symbol is not a good programming practice.
This can lead to duplicate definitions for an identifier. Also
it hinders the readability of our code.
The best approach is to import the function or functions you want, or to import the
complete module and use dot notation. This leads to clear code
that is easy to read and understand.
>>>import my_module
>>>import my_module
We can see that our code was executed only once. This means that
our module was imported only once.
Now, if our module changed during the course of the program, we would have
that reload it. One way to do this is to restart the interpreter. But this does not
helps a lot.
For example, we have defined an add() function in the example module that
as mentioned earlier.
>>> dir(example)
[’__builtins__’,
’__cached__’,
’__doc__’,
’__file__’,
__initializing__
’__loader__’,
__name__
’__package__’,
add
Here, we can see an ordered list of names (along with add). All the
Other names that begin with an underscore are default attributes
of Python associated with the module (we do not define them ourselves).
For example, the attribute __name__ contains the name of the module.
>>>importexample
>>> example.__name__
example
There are several ways to create an array, Numpy offers different commands for
according to the type of array that is desired to be created. Below are the co-
most important commands to create an array.
array([1, 2, 3])
The resize() command changes the shape and size of the array.
[Link](3,3)
myarray
array([[1, 2, 3],
[0,0,0]
[0, 0, 0]]
With the command array(), you can create a matrix using a list with repetitions.
actions.
[1, 2, 3, 1, 2, 3, 1, 2, 3]
[1,2,3,1,2,3,1,2,3]
Another way to repeat the elements of an array is to use the repeat command.
[Link]([1, 2, 3], 3)
array([1, 1, 1, 2, 2, 2, 3, 3, 3])
The command ones() returns a new array where its elements are value 1.
You must indicate the number of rows and the number of columns.
[Link]((3, 2))
array([[1., 1.],
[1., 1.]
[1., 1.]
The command zeros() returns a new array where its elements are zeros.
[Link]((2, 3))
The command eye() returns a matrix with a value of 1 on the diagonal and zeros elsewhere.
other positions.
myarray = [Link](3)
array([1, 1, 1])
The function arange() helps to record a sequence that has some initial value.
(start), algún valor final (stop), la diferencia entre los términos consecutivos (step)
and the data type (dtype).
[Link](start, stop, step, dtype)
a
array([3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23])
The linspace() function divides the given range into a specific number of elements.
and returns the sequence thus formed. The function takes the following parameters:
d
array([11., 12.6, 14.2, 15.8, 17.4, 19., 20.6, 22.2, 23.8,
25.4, 27.)
In the sequence generated by dividing the range 11-26 into 12 parts, the last argumen-
The result is the step (1.4545454545454546).
f=[Link](11, 27, 11, endpoint=False, retstep=True)
f
[11.0, 12.45454545, 13.90909091, 15.36363636]
16.01010102, 18.27272727, 19.72727273, 21.10101010,
22.63636364, 24.09090909, 25.54545455]), 1.4545454545454546
Name Description
append() Add an element to the end
insert() Add the element in the specified position. The function
it has two arguments: the first is the element and the
second is the position
count() This counts the number of times the argument is repeated.
pop() Remove the top element of the array
remove() Remove the element from a specified position
reverse() Invert the order of the elements in the array
tostring() Converts the given array into a string
To see the data type of the elements in the array, we use the command dtype.
[Link]
dtype('int32')
dtype('float32')
12.4. Operations
Let's remember that the arithmetic operators are: +,−,∗,/ y∗∗, to perform sums,
subtractions, multiplications, divisions, and powers at the element level. Next
some examples of operations with arrays.
x=[Link]([1, 2, 3])
y=[Link]([4, 5, 6])
print(x + y)Sum
[5 7 9]
print(x - y)Remaining
[-3 -3 -3]
print(x * y)multiplication
[1 2 3] * [4 5 6] = [4 10 18]
print(x / y)division
[1 2 3] / [4 5 6] = [0.25 0.4 0.5]
print(x**2)potentiation
[1 4 9]
r
32
[Link]
array([[ 4, 16],
[ 5, 25],
[ 6, 36]]
12.5. Broadcasting
Python applies element-wise operations not only in addition or subtraction,
but also in the case of multiplication and division, as shown in the
section 12.4, then the question arises What happens if two arrays are added
different dimensions? Python has a way of dealing with such situations:
This is called Broadcasting.
The array A is a two-dimensional array copying the elements of the first column in
all the other necessary columns, in this case 2 additional columns, to equal
the number of columns of array B. Similarly, the array B of columns is
it is converted into a two-dimensional array by copying the elements from the first one
row in all the other rows, in that example 3 additional rows are required. By
both array A and array B have the same dimension.
There is the following array 'm', on which different mathematical functions will be applied.
mathematics.
m = [Link]([-4, -2, 1, 3, 5])
Sum function.
[Link]()
In[1]: 3
Maximum function
[Link]()
In[2]: 5
Minimum function
[Link]()
In[3]:-4
Average function
[Link]()
0.6
For more detail and exploration of the added functions that NumPy has,
enter the link[Link]
12.7. Indexing
The standard indexing syntax in Python is: x[obj], where x is the array.
The selection of elements. There are 3 types of available indexing: basic
slicing, advanced indexing, field access
array([ 0, 1 4 9, 16, 25, 36, 49, 64, 81, 100, 121, 144])
If the beginning or end of the indexing is not indicated, it will be set by default.
nothing at the beginning / end of the matrix.
myarray[1:5]
array([ 1, 4, 9, 16])
myarray[-5::-2]
array([64, 36, 16, 4, 0])
array([[ 0, 1, 2, 3, 4, 5],
[ 6, 7, 8, 9, 10, 11],
[12, 13, 14, 15, 16, 17]
[18,19,20,21,22,23]
[24, 25, 26, 27, 28, 29],
[30, 31, 32, 33, 34, 35]])
The indexing syntax with brackets and commas is: array_name[row, column].
[2, 2]
14
r[:2, :-1]
array([[ 0, 1, 2, 3, 4],
[ 6, 7, 8, 9, 10]])
Example.
t = [Link]([[1,2],[3,4],[5,6]])
x[[0,1,2],[0,1,0]]
array([1,4,5])
Boolean indexing is used when the object is of the boolean type, the result is
returned by the comparison operators.
Example.
x = [Link]([1., -1., -2., 3])
x[x < 0] += 20
x
name=[’Harsh’,’Naved’,’Aman’,’Lovish’]
age=[100,70,24,18]
salary=[75500.00,65500.00,55500.00,45500.00]
data=[Link](4,dtype={’names’:(’name’,’age’,’salary’),
’formats’:(’U10’,’i4’,’f8’)})
data[’name’]=name
data[’age’]=age
data[’salary’]=salary
print(data)
#Output
Once a structured array has been created, data can be accessed from it.
the usual way. For example, to show the value of a particular attribute,
we can simply mention the name of the attribute in single quotes inside
from the brackets.
data[’name’]
#Output
data[’name’]
array([’Harsh’,’Naved’,’Aman’,’Lovish’], dtype=’<U10’)
data[’age’]
array([100, 70, 24, 18])
data[’salary’]
array([ 75500., 65500., 55500., 45500.])
data[1]
(’Naved’, 70, 65500.0)
13. Pandas
13.1. What is Pandas?
Pandas is a package that provides a comprehensive set of structures of
data for exploring, cleaning and manipulating data, as well as performing various analyses
statistical and financial.
∗ Reading/writing of many
different data formats.
∗ Selection of subsets of
data.
∗ Calculation between rows and columns.
∗ Application of operations to
independent data groups.
∗ Data transformation.
∗ Combination of several sets
data cough.
∗ Advanced functionality of se-
time beams.
∗ Visualization through mat-
plotlib and seaborn.
To use the functions of Pandas, it is required to import the package in your environment.
IPython:
import pandas as pd
import numpy as np
The two main data structures in Pandas are: Series and DataFrame.
13.2. Series
Series is the main component of Pandas and represents a labeled array.
unidimensional based on [Link] expands the functionality
delndarrayNumPy adding a set of associated labels that are used
to index the elements of the array.
For example, the following command creates a Series object of 100 random numbers.
normally distributed.
In [1]:
[Link](1)
s = [Link]([Link](100))
s
Out[1]:
0 1.624345
1 -0.611756
2 -0.528172
3 -1.072969
...
96 -0.343854
97 0.043597
98 -0.620001
99 0.698032
Length: 100, dtype: float64
Out[2]:
-0.528171752263
Out[3]:
2 -0.528172
5 -2.301539
20 -1.100619
dtype: float64
Note that slicing returns not only the values but also each element.
index (index label and value) of the series with the specified labels.
In [7]:
[Link]()
Out[5]:
95 0.077340
96 -0.343854
97 0.043597
98 -0.620001
99 0.698032
dtype: float64
When a Series object is created and values are not explicitly set
from the index label using the [Link] function, Pandas will assign va-
whole sequential lores starting from 0. To specify index labels not
defaults, use the index argument of the function or assign them using
the [Link] after creating the series.
In [7]:
s2 = [Link]([1, 2, 3, 4], index=[’a’,’b’,’c’,’d’])
s2
Out[7]:
a 1
b 2
c 3
d 4 dtype: int64
Out[8]:
a 1
b 2
c 3
d 4
e 5
dtype: int64
Out[9]:
10
The number of rows of an object Series that do not have a NaN value can be
determine with the function .count(). The function [Link] creates a NaN value.
In [11]:
[Link]()
Out[11]:
9
The result obtained in Series is very different from what would be obtained if
I would add a pure NumPy ndarray. NumPy would add the elements in
identical positions of each array.
Out[14]:
a 1
b 2
c 3
d 4
dtype: int64
In [15]:
s4 = [Link]([4, 3, 2, 1], index=['d', 'c', 'b', 'a'])
s4
Out[15]:
d 4
c 3
b 2
a 1
dtype: int64
Out[15]:
a 2
b 4
c 6
d 8
dtype: int64
A DataFrame has two axes, horizontal and vertical. The functions of Pandas are
they can be applied to either of the two axes, essentially indicating that it applies
to all the values of the selected rows or to all the items of the columns
specified.
Out[16]:
0 1
0 10 11
1 20 21
Each row of the array forms a row in the DataFrame. Since we did not specify a
index, Pandas creates a default int64 index in the same way as a
Series. Since we also do not specify the column names, Pandas also
assign the names for each column with a zero-based integer series.
Out[17]:
0 1 2 3 4
0 10 11 12 13 14
1 15 16 17 18 19
Out[19]:
a b
0 10 11
1 20 21
Likewise, you can access or change the names of the columns of a DataFrame.
with the [Link]. For example:
In [20]:
[Link]
Out[20]:
Index([u’a’,u’b’], dtype=’object’)
In [21]:
[Link] = ['c1', 'c2']
df
Out[26]:
c1 c2
0 10 11
1 20 21
Out[22]:
c1 c2
r1 0 1
r2 2 3
Out[23]:
c1 c2
0 1 6
1 2 7
2 3 8
3 4 9
4 5 10
In [24]:
s3 = [Link]([Link](12, 14), index=[1, 2])
[Link]({’c1’: s1,’c2’: s2,’c3’: s3})
Out[24]:
c1 c2 c3
0 1 6 NaN
1 2 7 12
2 3 8 13
3 4 9 NaN
4 5 10 NaN
In the previous example, it can be seen that the first two Series objects did not have a
index specified by which both were indexed with 0 to 4. The third series
it has index values; therefore, the values for those indexes are placed in
The DataFrame is in the row with the corresponding index of the previous columns.
Then, Pandas automatically fills with NaN for the values that do not
were supplied.
Another way to create a DataFrame is to import data from a csv file. Excel, for
the function read_csv() or read_excel() is used respectively. For ex-
import external files in other formats check the link[Link]
docs/stable/reference/[Link].
Out[25]:
Sector Price Book Value
Symbol
MMM Industrials 141.14 26.668
ABT Health Care 39.60 15.573
ABBV Health Care 53.95 2.954
ACN Information Technology 79.79 8.326
ACE Financials 102.91 86.897
Out[26]:
Price Book Value
Symbol
MMM 141.14 26.668
ABT 39.60 15.573
ABBV 53.95 2.954
You can also select multiple columns by the name of the columns.
For example:
In [27]:
sp500[[’Price’,’Sector’]]
Out[27]:
Price Sector
Symbol
MMM 141.14 Industrials
ABT 39.60 Health Care
ABBV 53.95 Health Care
ACN 79.79 Information Technology
... ... ...
YUM 74.77 Consumer Discretionary
ZMH 101.84 Health Care
ZION 28.43 Financials
ZTS 30.53 Health Care
[500 rows x 2 columns]
Out[28]:
Symbol
MMM 141.14
ABT 39.60
ABBV 53.95
ACN 79.79
...
YUM 74.77
ZMH 101.84
ZION 28.43
ZTS 30.53
Name: Price, dtype: float64
Examples:
In [29]:
sp500[:3]
Out[29]:
Sector Price Book Value
Symbol
MMM Industrials 141.14 26.668
ABT Health Care 39.60 15.573
ABBV Health Care 53.95 2.954
In [30]:
sp500[’XYL’:’YUM’]
Out[30]:
Sector Price Book Value
Symbol
XYL Industrials 38.42 12.127
YHOO Information Technology 35.02 12.768
YUM Consumer Discretionary 74.77 5.147
Out[31]:
Sector Price Book Value
Symbol
MMM Industrials 141.14 26.668
MSFT Information Technology 40.12 10.584
Out[32]:
Sector Price Book Value
Symbol
MMM Industrials 141.14 26.668
ABBV Health Care 53.95 2.954
Out[33]:
(0, 10)
In [34]:
[Link][[i1, i2]]
Out[34]:
Sector Price Book Value
Symbol
MMM Industrials 141.14 26.668
A Health Care 56.18 16.928
Out[35]:
Sector Price Book Value
Symbol
MSFT Information Technology 40.12 10.584
ZTS Health Care 30.53 2.150
Out[36]:
Sector Price Book Value
Symbol
A Health Care 56.18 16.928
GIS Consumer Staples 53.81 10.236
TRV Financials 92.86 73.056
Out[37]:
141.14
Out[38]:
141.14
Out[39]:
Symbol
MMM False
ABT True
ABBV True
ACN True
...
YUM True
ZMH False
ZION True
ZTS True
Name: Price, Length: 500, dtype:bool
This yields a Series that can be used to select rows where the value
esTrue:
In [40]:
sp500[[Link] < 100]
Out[40]:
Sector Price Book Value
Symbol
ABT Health Care 39.60 15.573
ABBV Health Care 53.95 2.954
ACN Information Technology 79.79 8.326
ADBE Information Technology 64.30 13.262
... ... ... ...
YHOO Information Technology 35.02 12.768
YUM Consumer Discretionary 74.77 5.147
ZION Financials 28.43 30.191
ZTS Health Care 30.53 2.150
Multiple conditions can be combined using parentheses and at the same time ...
select only a subset of the columns. The following command retrieves the
symbols and the price of all stocks with a price below 10 and above
0:
In [41]:
sp500[([Link] < 10) & ([Link] > 0)] [[‘Price’]]
Out[41]:
Symbol Price
FTR 5.81
HCBK 9.80
HBAN 9.10
Hello 8.82
WIN 9.38
By default, any arithmetic operation will be applied to all rows and columns.
from a DataFrame and will return a new DataFrame with the results (leaving the
original without changes).
When performing an operation between a DataFrame and a Series, Pandas will align the
index of Series along the columns of the DataFrame, which is called
transmission in line.
For example:
In [42]:
[Link](123456)
df = [Link]([Link](5, 4),
columns=[’A’,’B’,’C’,’D’])
In [42]:
df - [Link][0]
An arithmetic operation between two DataFrame objects will align with the labels.
stack of columns and indices.
For example: extract a small portion from a DataFrame and subtract from a DataFrame.
The result shows that the aligned values are subtracted from a0, while the
others adjust to NaN:
In [43]:
subframe = df[1:4][[’B’,’C’]]
subframe
Out[43]:
B C
1 -0.173215 0.119209
2 -2.104569 -0.494929
1 -0.706771 -1.039575
In [44]:
df subframe
Out[44]:
A B C D
0 NaN NaN NaN NaN
1 NaN 0 0 NaN
2 NaN 0 0 NaN
3 NaN 0 0 NaN
4 NaN NaN NaN NaN
Reindexing an object Series. For example, the following series has an index
con valores numéricos, y el índice es modificado a alfabético mediante una simple
assignment of a list of characters to the [Link], making the
values can be accessed through the character labels in the new
index.
In [46]:
[Link](1)
s = [Link]([Link](5))
s
Out[46]:
0 1.624345
1 -0.611756
2 -0.528172
3 -1.072969
4 0.865408
dtype: float64
Out[47]:
a 1.624345
-0.611756
c -0.528172
-1.072969
0.865408
dtype: float64
Out[48]:
a 0.000000
c -0.528172
e 0.865408
g NaN
dtype: float64
Reindexing is also useful when you want to align two series to perform a
matching operation between the elements of each series, but for some reason
The reason is that both series had index labels that did not align initially.
For example, the first objectSeries has indices like sequential integers.
but the second has a string representation of what would be numbers
sequential integers. The addition of both Series has the following result,
where everyone is NaNs and there are repeated label values:
In [49]:
s1 = [Link]([0, 1, 2], index=[0, 1, 2])
s2 = [Link]([3, 4, 5], index=[’0’,’1’,’2’])
s1 + s2
Out[49]:
0 NaN
1 NaN
2 NaN
0 NaN
1 NaN
2 NaN
dtype: float64
In this case, Pandas first try to align with the indices and when they do not find
matches, copy the index labels of the first object Series they attempt
add the indices of the second objectSeries. But since they are of another type, they
it resets to a zero-based whole sequence, which gives
como resultado valores duplicados. Y finalmente, todos los valores resultantes son
NaN because the operation tries to add the item from the first series with the label
enter 0, which has the value 0 but cannot find the item from the other series
with the whole label 0; therefore, the result is NaN. This inconvenience is
solve with the reindexing of the second series, converting the values to aint:
In [50]:
[Link] = [Link](int)
s1 + s2
Out[50]:
0 3
1 5
2 7
dtype: int64
In [52]:
s2 = [Link]()
[Link](['a', 'f'], fill_value=0)
Out[51]:
a 1.624345
f 0.000000
dtype: float64
Out[52]:
0 red
3 green
5 blue
dtype:object
Out[53]:
0 red
1 red
2 red
3 green
4 green
5 blue
6 blue
dtype:object
Out[54]:
0 red
1 green
2 green
3 green 4 blue 5 blue 6 NaN dtype:object
We imported stock information for MSFT and AAPL from the year 2019 extracted from
Yahoo! Finance, to exemplify the concepts that will be explored in this section
from Pandas.
Remember that to import data from csv files, we use the function pd.read_csv().
And part of the data from the DataFrame is extracted for the purposes of the examples.
In [1]:
msft = pd.read_csv("[Link]", index_col=0, parse_dates=True)
aapl = pd.read_csv("[Link]", index_col=0, parse_dates=True)
In [2]:
msft[:3]
In [3]:
aapl[:3]
Pandas offers a wide variety of functions for concatenating, merging and reshaping.
stop the data. The following sections take us through various scenarios
common to each one, using stock data (examples: MSFT and AAPL).
For example:
In [4]:
msftA01 = msft[’2019-01’][[’Adj Close’]]
msftA02 = msft[’2019-02’][[’Adj Close’]]
msftA01[:3]
Out[4]:
In [5]:
msftA02[:3]
Out[5]:
Date Adj Close
2019-02-01 27.27
2019-02-02 27.32
2019-02-03 27.59
Out[6]:
The following example concatenates the first five adjusted closing values.
in January for both MSFT and AAPL. These have index labels
identical and result in duplicate index labels in the new
DataFrame. During a concatenation along the row axis, Pandas
the index labels will not be aligned. They will be copied and this may create labels
of duplicate and identical indices:
In [7]:
aaplA01 = aapl[’2019-01’][[’Adj Close’]]
withDups = [Link]([msftA01[:3], aaplA01[:3])
withDups
Out[7]:
Out[8]:
Date Adj Close
MSFT 2019-01-03 24.42
2019-01-04 25.00
2019-01-05 25.25
AAPL 2019-01-03 55.41
2019-01-04 55.71
2019-01-05 56.33
Out[9]:
The concatenation along the axis of the row can also be carried out.
using DataFrame objects with multiple columns. Likewise if
the argument key is not used, there will be duplicate rows.
In [10]:
msftAV = msft[[’Adj Close’,’Volume’]]
aaplAV = msft[['Adj Close', 'Volume']]
[Link]([msftAV, aaplAV])
Out[10]:
Out[11]:
Date Adj Close Volume
2019-01-03 24.42 64731500
2019-01-04 25.00 80516100
2019-01-05 25.25 56081400
2019-01-06 25.64 99455500
... ... ...
2019-12-24 70.72 NaN
2019-12-26 69.74 NaN
2019-12-27 70.02 NaN
2019-12-28 69.28 NaN
[498 rows x 2 columns]
Since the rows that originate from the DataFrame aaplAno have the
Volume column, the Pandas insert NaN in the Volume column for
those rows.
The set of columns that results from a concatenation along the axis
from the row is the result of relational algebra through the names of the
columns. In this default scenario, the resulting column is the union
of the column names of each DataFrame. This can be changed.
to an intersection using the dejoin parameter. For example:
In [12]:
[Link]([msftAV, aaplA], join=’inner’)
Out[12]:
Date Adj Close
2019-01-03 24.42
2019-01-04 25.00
2019-01-05 25.25
2019-01-06 25.64
... ...
2019-12-24 70.72
2019-12-26 69.74
2019-12-27 70.02
2019-12-28 69.28
[498 rows x 1 columns]
Out[13]:
Out[14]:
MSFT AAPL
Date Adj Close Volume Adj Close Volume
2019-01-03 24.42 64731500 55.41 75555200
2019-01-04 25.00 80516100 55.71 65005500
January 5, 2019 25.25 56081400 56.33 67817400
2019-01-06 25.64 99455500 NaN NaN
2019-01-09 25.31 59706800 NaN NaN
Just like with concatenation along the row axis, the type of
union made by [Link]() can be changed using the parameter
dejoin. The following command performs an inner join instead of a
external, which results in the intersection of the row index labels:
In [15]:
[Link]([msftA[:5], aaplA[:3]], axis=1,
join=’inner’, keys=[’MSFT’,’AAPL’])
Out[15]:
Date Adj Close Adj Close
2019-01-03 24.42 55.41
2019-01-04 25.00 55.71
2019-01-05 25.25 56.33
The resulting DataFrame only has three rows because of those index labels.
they were the only common ones in the two concatenated DataFrame objects.
If you want to ignore indices in the result of [Link](), you can use the
parameter ignore_index=True, which will not consider the index and will create a
zero-based integer index by default, as shown here:
In [16]:
[Link]([msftA[:3], aaplA[:3]], ignore_index=True)
Out[16]:
Adj Close
0 24.42
1 25.00
2 25.25
3 55.41
4 55.71
5 56.33
Out[17]:
Date Adjusted Close
0 2019-01-03 24.42
1 2019-01-04 25.00
2 2019-01-05 25.25
In [18]: msftVR[:3]
Out[18]:
Date Volume
0 2019-01-03 64731500
1 2019-01-04 80516100
2 2019-01-05 56081400
Out[19]:
Date Adj Close Volume
0 2019-01-03 24.42 64731500
1 2019-01-04 25.00 80516100
2 2019-01-05 25.25 56081400
3 2019-01-06 25.64 99455500
4 2019-01-09 25.31 59706800
Out[20]:
Date Adj Close
0 2019-01-03 24.42
1 2019-01-04 25.00
2 2019-01-05 25.25
3 2019-01-06 25.64
2019-01-09 25.31
In [21]:
msftVR2_4 = msftVR[2:4]
msftVR2_4
Out[21]:
Date Volume
2 2019-01-05 56081400
3 2019-01-06 99455500
Internal union (inner): since there are only two rows with matching dates,
the result only has two rows and merges the objectsDataFrame where the
Date values are common.
In [22]:
[Link](msftAR0_5, msftVR2_4)
Out[22]:
Date Adj Close Volume
0 2019-01-05 25.25 56081400
1 2019-01-06 25.64 99455500
Out[23]:
Date Adj Close Volume
0 2019-01-03 24.42 NaN
1 2019-01-04 25.00 NaN
2 2019-01-05 25.25 56081400
3 2019-01-06 25.64 99455500
4 2019-01-09 25.31 NaN
[Link]
Financial data is often stored in a format where the
data are not normalized and, therefore, have repeated values in mu-
columns or values that logically should exist in other tables. A
an example of this would be the following, where the historical prices for multi-
multiple actions are represented in a single DataFrame using a column
Symbol. The following code creates this schema.
In [24]:
[Link](0,'Symbol','MSFT')
[Link](0, 'Symbol', 'AAPL')
combined = [Link]([msft, aapl]).sort_index()
s4p = combined.reset_index();
s4p[:5]
Out[24]:
Date Symbol Open High Low Close Volume Adj Close
0 2019-01-03 MSFT 26.55 26.96 26.39 26.77 64731500 24.42
1 2019-01-03 AAPL 409.40 412.50 409.00 411.23 75555200 55.41
2 2019-01-04 MSFT 26.82 27.47 26.78 27.40 80516100 25.00
3 2019-01-04 AAPL 410.00 414.68 409.28 413.44 65005500 55.71
4 2019-01-05 MSFT 27.38 27.73 27.29 27.68 56081400 25.25
Out[25]:
Date AAPL MSFT
2019-01-03 55.41 24.42
2019-01-04 55.71 25.00
2019-01-05 56.33 25.25
This function takes all distinct values from the Symbol column, the
rotate into columns in the new DataFrame, and then include the values in those
columns from the AdjClose value for the specific symbol of the DataFrame
original.
4. Stacking (stack) and unstacking (unstack)
Out[26]:
Date Symbol
2019-01-03 AAPL 55.41
MSFT 24.42
2019-01-04 AAPL 55.71
MSFT 25.00
...
2019-12-27 AAPL 70.02
MSFT 25.29
2019-12-28 AAPL 69.28
MSFT 24.91
dtype: float64
This has created a new index with an additional level called Symbol. Each
the file is then indexed by Date and Symbol, and for each unique level of
Date and Symbol, Pandas has inserted the appropriate AdjClose value.
The result of this allows the efficient search for any value of
AdjClose using the index. For example
In [27]:
[Link]['2019-01-03','AAPL']
Out[27]:
55.41
Out[28]:
Symbol
AAPL 55.41
MSFT 24.42
dtype: float64
Out[29]:
Date
2019-01-03 24.42
2019-01-04 25.00
January 5, 2019 25.25
2019-01-06 25.64
...
2019-12-24 25.38
2019-12-26 25.20
2019-12-27 25.29
2019-12-28 24.91
dtype: float64
The function .unstack() performs the opposite function; that is, it pivots a
level of an index in a column of a new DataFrame. The following
command deactivates the last level of the MultiIndex and results in a DataFrame
equivalent to the original.
In [30]:
unstackedCloses = [Link]()
unstackedCloses[:3]
Out[30]:
5. Melting
Out[31]:
Date Symbol variable value
0 2019-01-03 MSFT Open 26.55
1 2019-01-03 AAPL Open 409.40
2 2019-01-04 MSFT Open 26.82
3 2019-01-04 AAPL Open 410.00
4 2019-01-05 MSFT Open 27.38
Out[32]:
Date Symbol variable value
0 2019-01-03 MSFT Open 26.55
498 2019-01-03 MSFT High 26.96
996 2019-01-03 MSFT Low 26.39
1494 2019-01-03 MSFT Close 26.77
1992 2019-01-03 MSFT Volume 64731500.00
2490 2019-01-03 MSFT Adj Close 24.42
Divide
Objects in Pandas are divided into groups using the .groupby() method.
We previously reorganized the data from the s4g DataFrame.
In [33]:
s4g = combined[['Symbol', 'AdjClose']].reset_index()
[Link](1, 'Year', [Link](s4g['Date']).year)
[Link](2, 'Month[:5]', [Link](s4g['Date']).month)
s4g[:5]
Out[33]:
Date Year Month Symbol Adj Close
0 2019-01-03 2019 1 MSFT 24.42
1 2019-01-03 2019 1 AAPL 55.41
2 2019-01-04 2019 1 MSFT 25.00
3 2019-01-04 2019 1 AAPL 55.71
4 2019-01-05 2019 1 MSFT 25.25
These data differ from the previous ones, as only the AdjClose value is used.
and the Date column is split into two other columns, Year and Month. This
The division of the date is made to be able to provide the capability to group.
the data by Month and Year for each Symbol variable.
These data consist of four categorical variables and one continuous variable.
AdjC Close. In Pandas, it is possible to group by any categorical variable.
indicating your name in the [Link](). For example:
In [34]:
[Link]('Symbol')
Out[34]:
<[Link] object at 0x7ffaeeb49a10>
Out[35]:
dict
In [36]:
[Link]
Out[36]:
{u’AAPL’: [1, 3, 5, 7, 9, 11, 13, 14, 16, 18, 20, 23, 25, 27, 29,
30, 33, 34, 37, 38, 41, 43, 45, 46, 48, 50, 53, 54, 56, 58, 61,
63, 64, 67, 69, 71, 72, 75, 77, 79, 81, 82, 84, 89, 91, 92, 94,
...
452, 455, 456, 458, 460, 463, 464, 466, 468, 471, 472, 474, 476,
478, 480, 482, 484, 487, 488, 490, 492, 494, 497]
’MSFT’: [0, 2, 4, 6, 10, 12, 15, 17, 19, 21, 22, 24, 26, 28, 31, 32,
...
477, 479, 481, 483, 485, 486, 489, 491, 493, 495, 496]}
The Python function len() can be used to return the number of groups.
that will result from the grouping. You can also use the property .ngroups:
In [37]:
len(grouped), [Link]
Out[37]:
(2, 2)
The division is not carried out until some type of action is taken on it.
objectGroupBy. However, it is possible to iterate over several properties of the
object to see how the data will be grouped (which forces them to be grouped).
In [38]:
def print_groups(group_object):
forname, groupingroupobject:
print(name)
print([Link]())
In [39]:
print_groups(grouped)
Out[39]:
AAPL
Date Year Month Symbol Adj Close
1 2019-01-03 2019 1 AAPL 55.41
3 2019-01-04 2019 1 AAPL 55.71
5 2019-01-05 2019 1 AAPL 56.33
7 2019-01-06 2019 1 AAPL 56.92
9 2019-01-09 2019 1 AAPL 56.83
MSFT
Date Year Month Symbol Adj Close
0 2019-01-03 2019 1 MSFT 24.42
2 2019-01-04 2019 1 MSFT 25.00
4 2019-01-05 2019 1 MSFT 25.25
6 2019-01-06 2019 1 MSFT 25.64
8 2019-01-09 2019 1 MSFT 25.31
It is possible to create, for each distinct value in the Symbol column of the DataFrame
original, a group that consists of a DataFrame. Then it copies the columns
and the ungrouped data in each of those DataFrame objects and then
use the command of the specified columns as the group name.
The .size() function of the object provides a good summary of the size of all the
groups:
In [40]:
[Link]()
Out[40]:
Symbol
AAPL 249
MSFT 249
dtype: int64
If you want the data of the elements of a specific group, you can use
the property.get_group(). For example: retrieve the MSFT group.
In [41]:
grouped.get_group('MSFT')
Out[41]:
Date Year Month Symbol Adj Close
0 2019-01-03 2019 1 MSFT 24.42
2 2019-01-04 2019 1 MSFT 25.00
4 January 5, 2019 MSFT 25.25
6 January 6, 2019 MSFT 25.64
.. ... ... ... ... ...
491 2019-12-24 2019 12 MSFT 25.38
493 2019-12-26 2019 12 MSFT 25.20
495 2019-12-27 2019 12 MSFT 25.29
496 2019-12-28 2019 12 MSFT 24.91
[249 rows x 5 columns]
Grouping can be done on multiple columns by passing a list of
column names. The following command groups the data by the variables
Symbol, Year and Month:
In [42]:
mcg = [Link](['Symbol', 'Year', 'Month'])
print_groups(mcg)
Out[42]:
Date Year Month Symbol Adj Close
1 2019-01-03 2019 1 AAPL 55.41
3 2019-01-04 2019 1 AAPL 55.71
5 2019-01-05 2019 1 AAPL 56.33
7 2019-01-06 2019 1 AAPL 56.92
9 2019-01-09 2019 1 AAPL 56.83
In [43]:
mi = s4g.set_index(['Symbol','Year','Month'])
my
Out[43]:
Now you can perform grouping using the levels of the hierarchical index.
quico. The following groups by index level 0.
In [44]:
mig_l1 = [Link](level=0)
print_groups(mig_l1)
Out[44]:
Symbol Year Month Date Adj Close
AAPL 2019 1 2019-01-03 55.41
1 2019-01-04 55.71
1 2019-01-05 56.33
1 2019-01-06 56.92
1 2019-01-09 56.83
Out[45]:
Symbol Year Month Date Adj Close
AAPL 2019 1 2019-01-03 55.41
1 2019-01-04 55.71
1 2019-01-05 56.33
1 2019-01-06 56.92
1 2019-01-09 56.83
...
[Link]
Out[46]:
The result of the aggregation will have an index identical to that of the original data.
If you do not want this to happen, you can use the option as_index=False.
[Link]() to specify that the index structure is not duplicated.
In [47]:
[Link](['Symbol', 'Year', 'Month'],
as_index=False).agg([Link])[:5
Out[47]:
Out[48]:
Symbol Year Month mean std
AAPL 2019 1 57.75 1.80
2 67.05 3.57
3 77.82 4.16
4 81.66 3.06
... ... ...
MSFT 2019 9 28.64 0.43
10 27.04 0.67
11 26.00 1.00
12 25.31 0.36
[24 rows x 2 columns]
14. Matplotlib
14.1. Introduction
Matplotlib is a package for creating graphs and visualizing data. The architecture
matplotlib is structured in three layers, which are placed at three different levels.
Communication is unidirectional, meaning each layer can communicate with the layer
underlying.
Backend Layer: the layer that operates at the lowest level is the backend layer.
This layer contains the matplotlib APIs and a set of classes that play the role
of the implementation of graphic elements at a low level.
Artist Layer: in this layer are all the elements that make up a
graphic, such as the title, axis labels, markers, etc. Each element of a
The graphic corresponds to this artistic layer.
Scripting Layer (pyplot): for calculation purposes, and in particular for analysis
and data visualization, the interface layer called pyplot is the best. These
functions provide Python programmers with capabilities similar to those of
MATLAB.
14.2. Pyplot
The pyplot module is a set of functions that allow you to use matplotlib.
very similar to MATLAB.
It will start with a simple graphic, importing the pyplot module, which in general
it is renamed comoplt. Therefore, you can simply pass the values you want
represent as a sequence of integers and these are associated with the sequence
natural values of the axis: 0,1,2,3.... See the following example:
Icon Description
The size of the axes fits perfectly within the range of the input data.
The objects that make up a graph have many attributes that characterize them. All
the attributes are default values, but they can be configured using
arguments, often referred to as kwargs. These keywords are defined as
arguments to functions. Example of a line graph.
[Link](*args, **kwargs)
In [4]: [Link]([1,2,4,2,1,0,1,2,1,4], linewidth=2.0)
Example of subgraphics.
In [5]: t = [Link](0,5,0.1)
y1 = [Link](2*[Link]*t)
y2 = [Link](2*[Link]*t)
In [6]: [Link](211)
[Link](t,y1,'b-.')
[Link](212)
[Link](t, y2, 'r--')
Out[6]: [<[Link].Line2D at 0xd47f518>]
The same can be done by dividing the figure into two horizontal subgraphs. The
The sentence and arguments are as follows:
In [7]: t = [Link](0., 1., 0.05)
y1 = [Link](2*[Link]*t)
y2 = [Link](2*[Link]*t)
In [8]: [Link](121)
[Link](t, y1, 'b-.')
[Link](122)
[Link](t,y2,'r--')
[<[Link].Line2D at 0xed0c208>]
[Link]
To include a title in a graph, the title() function is used.
It is also possible to modify the color of the axis labels, modify the title.
changing the font and increasing the size of the characters to emphasize the title
of the graphic, as shown below:
In [10]: [Link]([0,5,0,20])
[Link](’My first plot’,fontsize=20,fontname=’Times New Roman’)
[Link]('Counting', color='gray')
[Link]('Square values', color='gray')
[Link]([1,2,3,4],[1,4,9,16],'ro')
Out[10]: [<[Link].Line2D at 0x11f17470>]
In addition, matplotlib is not limited to this: pyplot allows you to add text to any
position within a graph. This feature is performed by the function called
text(). The syntax is as follows:
As can be seen in the previous figure, each point on the graph has a labeling.
quiet. In addition, it is possible to include expressions in LaTeX, therefore it allows inserting
mathematical expressions in the graph. To do this, a LaTeX expression can be added.
the text between two characters $. Generally, it is necessary to precede the string that contains
LaTeX expressions with unar, which indicates raw text, to avoid sequences of
unintended escapes. Therefore, as an example, the formula can be added
what describes the trend followed by the point of the graph in the previous example and to be
enclosed in a colored frame.
Example of a graphic with LaTeX text.
In [12]: [Link]([0,5,0,20])
[Link](’My first plot’,fontsize=20,fontname=’Times New Roman’)
[Link]('Counting', color='gray')
[Link]('Square values', color='gray')
[Link](1,1.5,'First')
[Link](2,4.5,'Second')
[Link](3,9.5,'Third')
[Link](4,16.5,'Fourth')
[Link](1.1,12,r'$y = x^2$',fontsize=20,bbox={'facecolor':'yellow',
’alpha’:0.2})
[Link]([1,2,3,4],[1,4,9,16],'ro')
Out[12]: [<[Link].Line2D at 0x13920860>]
14.4.2. Grid
Another element that can be added to a graph is a grid. Often, its
addition is necessary to better understand the position that each point has in the
chart. Adding a grid can be done using the function grid() with the
argumentoTrue.
Example of a graph with a grid.
In [13]: [Link]([0,5,0,20])
[Link](’My first plot’,fontsize=20,fontname=’Times New Roman’)
[Link]('Counting', color='gray')
[Link]('Square values', color='gray')
[Link](1,1.5,'First')
[Link](2,4.5,'Second')
Third
[Link](4,16.5,'Fourth')
[Link](1.1,12,r'$y = x^2$',fontsize=20,
bbox={’facecolor’:’yellow’,’alpha’:0.2})
[Link](True)
[Link]([1,2,3,4],[1,4,9,16],'ro')
Out[13]: [<[Link].Line2D at 0x10f76898>]
14.4.3. Legend
Another very important component that must be present in any graph is
the [Link] provides the function legend() to add a legend
the graphic and a string that indicates the words with which you want them to be displayed
series.
For example, the name "First series" is assigned to the graphed data.
In [14]: [Link]([0,5,0,20])
[Link](’My first plot’,fontsize=20,fontname=’Times New Roman’)
[Link]('Counting', color='gray')
[Link]('Square values', color='gray')
[Link](2, 4.5, 'Second')
[Link](3,9.5,'Third')
Fourth
$y =x^2$
’alpha’:0.2})
[Link](True)
[Link]([1,2,3,4],[1,4,9,16],’ro’)
[Link](['First series'])
Out[14]: <[Link] at 0x16377550>
As shown in the previous figure, the legend is included in the upper corner.
default to the right. If you want to change this behavior, you must add some
kwargs. The position occupied by the legend is set by assigning numbers from 0 to 10.
alquargde blockage. Each of these numbers characterizes one of the corners of
graphic. A value of 1 is the default value, that is, the upper right corner. A
the following is the localization table to configure the position of the legend.
In the following example, several series are graphed, each characterized by the function
plot() and the order in which they are defined correspond to the order of the text labels.
defined as arguments in the legend() function.
y= sine(3∗ x)/x
Therefore, two numeric matrices are created.
Matrix that contains the dex values: to define a sequence of values
Clients will use the function [Link](), and given that the mathematical function is
sinusoidal, reference should be made to values that are multiples and submultiples of
pi ([Link]).
Matrix that contains the values of y: the values of y can be obtained by applying
the function [Link]() directly to these values.
Finally, the graph is created with the function plot().
In [16]: import [Link] as plt
import numpy as np
x = [Link](-2*[Link], 2*[Link], 0.01)
y = [Link](3*x)/x
[Link](x,y)
Out[16]: [<[Link].Line2D at 0x22404358>]
As another example, the case of the mathematical function can be expanded to the following
equation:
y= sin(n∗ x)/x
14.5.3. Histograms
A histogram consists of adjacent rectangles on the x-axis, divided into intervals
discrete called "bins", and with an area proportional to the frequency of occurrences
To represent a histogram, pyplot provides a special function called
hist(). This plotting function has a characteristic that other functions do not have. The
funcionhist(), besides drawing the histogram, returns a tuple of values that are
the results of the histogram calculation. The hist() function can perform the calculation
of the histogram, that is, it is sufficient to provide a series of sample values.
as an argument and the number of locations to be divided, and the function
will be responsible for dividing the sample range into many intervals, and then the calculations will be made
occurrences for each "bins".
Example: An array of 100 random values from 0 to 100 is generated using the function
aleatoriarandint(), and it is divided into 20 "bins". If the "bins" are not specified, the value
The default is 10.
In the previous graph, it can be seen that the indices are drawn on the x-axis.
dialog box at the beginning of each bar. In reality, because each bar corresponds
to a category, it would be better if the categories are specified through the label of
verification, defined by a list of text strings with the function xticks().
In [22]: import numpy as np
index = [Link](5)
values1 = [5,7,3,4,6]
[Link](index, values1)
[Link](index+0.4,['A','B','C','D','E'])
There are many options to further refine the bar chart. Each one
These details are configured by adding a specific 'unkwarge' as an argument in the
function bar() .
For example, the standard deviation values of the bar can be added through
delyerr kwarg together with a list that contains the standard deviations. This kwarg
it is usually combined with another kwarg called error_kw, which in turn accepts other kwargs
specialized to represent error bars. Two very specific kwargs used
in this case soneColor, which specifies the color of the error bars, and capsize, which
define the width of the cross lines that mark the ends of the error bars.
Another kwarg you can use is alpha, which indicates the degree of transparency of the
color bar, alpha is a value that ranges from 0 to 1. When this value is 0, the object is
completely transparent to gradually become more significant with the increase
of the value, until reaching 1. As usual, it is recommended to use a legend,
so in this case the legend() argument should be used to identify the series that
it is being represented.
Example of a bar graph with error bars.
In [23]: import numpy as np
index = [Link](5)
values1 = [5,7,3,4,6]
std1 = [0.8,1,0.4,0.9,1.3]
A Bar Chart
[Link](index, values1, yerr=std1, error_kw={'ecolor': '0.1',
’capsize’:6},alpha=0.7,label=’First’)
[Link](index+0.4,[ 'A', 'B', 'C', 'D', 'E' ])
[Link](loc=2)
Regarding the horizontal multi-series bar chart, the code is very similar, only
the function bar() must be replaced by the corresponding function barh() and do not forget
replace the xticks() function with the yticks() function. It is necessary to reverse the range
of values that cover the axes in the function axis().
Example of a horizontal multiseries bar chart.
In [26]: import [Link] as plt
import numpy as np
index = [Link](5)
values1 = [5,7,3,4,6]
values2 = [6,6,4,5,7]
values3 = [5,6,5,4,6]
bw = 0.3
[Link]([0,8,0,5])
[Link](’A Multiseries Horizontal Bar Chart’,fontsize=20)
[Link](index, values1, bw, color='b')
[Link](index+bw, values2, bw, color='g')
[Link](index+2*bw,values3,bw,color='r')
[Link](index + 0.4, ['A', 'B', 'C', 'D', 'E'])
Regarding the horizontal bar chart, the same rules can be applied,
just remember to set establishbarh() as the value of the kwarg type.
To create the equivalent horizontal stacked bar chart, the must be replaced
function bar() for the function barh(), taking into account to also change the others.
parameters. In fact, the function xticks() should be replaced by the function
yticks(), since the category labels must now be reported on the y-axis.
After making all these changes, you will get the horizontal bar chart api-
side as shown below, as an example:
In [29]: import [Link] as plt
import numpy as np
index = [Link](4)
series1 = [Link]([3,4,5,3])
series2 = [Link]([1,2,2,5])
series3 = [Link]([2,3,3,4])
[Link]([0,15,-0.5,3.5])
A Multiseries Horizontal Stacked Bar Chart
[Link](index, series1, color='r')
[Link](index, series2, color='g', left=series1)
[Link](index, series3, color='b', left=(series1 + series2))
[Link](index+0.4,[‘Jan18’,‘Feb18’,‘Mar18’,‘Apr18’])
For example:
In [31]: import [Link] as plt
import pandas as pd
data = {’series1’:[1,3,4,3,5],
’series2’:[2,4,5,2,4],
’series3’:[3,2,3,1,3]}
df = [Link](data)
[Link](kind='bar', stacked=True)
Out[31]: <[Link]._subplots.AxesSubplot at 0xcda8f98>
Below is an example.
In [32]: import [Link] as plt
x0 = [Link](8)
y1 = [Link]([1,3,4,6,4,3,2,1])
y2 = [Link]([1,2,5,4,3,3,2,1])
[Link](-7,7)
[Link](x0, y1, 0.9, facecolor='r')
[Link](x0, -y2, 0.9, facecolor='b')
[Link](())
[Link](True)
for x, y in zip(x0, y1):
[Link](x + 0.4, y + 0.05, '%d' % y, ha='center', va='bottom')
To add complexity to the pie chart, you can draw it with a part extracted from
pie. For this, there is a special kwarg called explode. It is nothing more than a sequence
of values 0 or 1, where 1 corresponds to the fully extended portion and 0
corresponds to the complete parts in the foot. The angle can also be adjusted.
rotation of the figure adding the kwarg:startangle, which takes an integer value between
0 and 360, which are the degrees of rotation with precision, where 0 is the default value.
Example of a pie chart with a part extracted.
In [34]: import [Link] as plt
labels = [’Nokia’,’Samsung’,’Apple’,’Lumia’]
values = [10,30,45,15]
colors = [’yellow’,’green’,’red’,’blue’]
explode = [0.3,0,0,0]
A Pie Chart
[Link](values, labels=labels, colors=colors, explode=explode,
startangle=180)
[Link]('equal')
Moreover, the kwarg:autopct can be used, which adds to the center of each part of the
draw a text label that shows the corresponding value. Finally, one can
add a shadow the Kwarg: shadow configuring it to True. For example:
In [35]: import [Link] as plt
labels = [’Nokia’,’Samsung’,’Apple’,’Lumia’]
values = [10,30,45,15]
colors = [’yellow’,’green’,’red’,’blue’]
explode = [0.3,0,0,0]
A Pie Chart
[Link](values, labels=labels, colors=colors, explode=explode,
shadow=True, autopct='%1.1f%%', startangle=180)
[Link]('equal')
This is a chart that has characteristics of both the pie chart and the chart.
of bars. In fact, like the pie chart, the angle of each sector provides information.
percentage represented by that category with respect to the total. Regarding the graph of
bars, the radial graph is the numerical value of that category.
6 For more details, please check:
[Link]
[Link]
Para obtener un gráfico polar se debe usar la funciónbar()e indicar la lista que
contains the angles θ and a list of the radial extent of each sector.
It is important to remember that the color of the graph can be defined with a list of
string values containing RGB codes in the format #rrggbb
to the colors that are desired.
14.6. 3D Graphics
The mplot3d module also uses the Figure object, only that instead of axes it will define
a new type of object, called Axes3D. Therefore, it is necessary to import this module
to use the Axes3D object.
from mpl_toolkits.mplot3d import Axes3D
14.6.1. 3D Surface
With the paquetemplot3D, surfaces can be drawn directly in 3D.
For example, the function z = f(x, y) is graphed, and the surface can be seen with the function.
plot_surface().
In [39]: from mpl_toolkits.mplot3d import Axes3D
import [Link] as plt
fig = [Link]()
ax = Axes3D(fig)
X = [Link](-2, 2, 0.1)
Y = [Link](-2, 2, 0.1)
X,Y = [Link](X,Y)
deff(x,y):
return(1 - y**5 + x**5)*[Link](-x**2-y**2)
ax.plot_surface(X,Y,f(X,Y), rstride=1, cstride=1)
A 3D surface stands out more by changing the color map, for example, it can be
set the kwarg cmap option. You can also rotate the surface using the function
view_init(). In fact, this adjusts the viewpoint from which the surface is seen,
changing the kwargs: elevyazim. Through their combination one can obtain the
surface shown from any angle. The first kwarg adjusts the height at which it
it sees the surface, while the second adjusts the rotation angle of the surface.
Below is an example.
In [40]: from mpl_toolkits.mplot3d import Axes3D
import [Link] as plt
fig = [Link]()
ax = Axes3D(fig)
X = [Link](-2,2,0.1)
Y = [Link](-2, 2, 0.1)
X,Y = [Link](X,Y)
def(x,y): return(1 - y**5 + x**5)*[Link](-x**2-y**2)
ax.plot_surface(X,Y,f(X,Y), rstride=1, cstride=1, cmap=[Link])
ax.view_init(elev=30, azim=125)
[Link](x,y,0,zdir='y',color=clr)
[Link](x, y2, 10, zdir='y', color=clr)
[Link](x, y3, 20, zdir='y', color=clr)
[Link](x,y4,30,zdir='y',color=clr)
[Link](x,y5,40,zdir='y',color=clr)
ax.set_xlabel('X Axis')
ax.set_ylabel('Y Axis')
ax.set_zlabel('Z Axis')
ax.view_init(elev=40)
Tools
Time Series analysis tsa
The Datasets Package
[Link] statistics
This package has a set of functions for descriptive statistics, co-
my part of the data exploration. Below is a list of the
main functions:
Functions Description
[Link](x[, unbiased, demean, fft, ...]) Autocovariance for 1D
[Link](x[, unbiased, nlags, qstat, . . . ]) Autocorrelation function for 1d arrays.
[Link](x[, nlags, method, alpha]) Estimated partial autocorrelation
stattools.pacf_ols(x[, nlags, efficient, . . . ]) Calculate partial autocorrelations using
OLS
[Link](x, y[, unbiased]) cross-correlation function for 1d
[Link](X) Return the periodogram for the frequency
natural of X
[Link](x[, maxlag, regression, . . . ]) Unit root test Dickey-Fuller
augmented
[Link](x[, regression, lags, store]) Stationarity test of
Kwiatkowski-Phillips-Schmidt-Shin
[Link](y0, y1[, trend, method, . . . ]) Test of no cointegration of an equation
univariate
stattools.q_stat(x, nobs[, type]) Statistics of Return’s Ljung-Box Q
stattools.levinson_durbin(s[, nlags, isacov]) Levinson-Durbin recursion for
autoregressive processes
stattools.innovations_algo(acov[, nobs, rtol]) Innovation algorithm for conversion
autocovariances to MA parameters
stattools.levinson_durbin_pacf(pacf[, nlags]) Levinson-Durbin algorithm that returns the
ACF and AR coefficients
stattools.arma_order_select_ic(y[, max_ar, ...]) Returns the information criteria for
many ARMA models
Interpolation
[Link] is a method used for
convert low frequency data to high frequency. The Denton method minimizes
the distance given by the penalty function, with least squares, between
the unknown reference series and the series of indicators subject to the condition
that the sum of the reference series is equal to the reference. If not available
from no reference point for the latest observations of the indicators,
the extrapolation is carried out using the latest reference indicator ratio of the
previous period.
Mathematically it is given:
sum(X) = A
For each period, whereXit is the reference series,Iit is the indicator andAis the
reference point.
Example:
indicator = [50,100,150,100] * 5
benchmark = [500,400,300,400,500]
benchmarked = dentonm(indicator, benchmark, freq="aq")
[Link]
This principal component analysis module has the following parameters
tros:
Example
Note that the main components are calculated using SVD and therefore
the correlation matrix is never built unless the
method'eig'.
Methods Description
plot_rsquare([ncomp, ax]) Boxplots of the individual series
R-squared against the number of PCs
Eigenvalue plot
ordered
project([ncomp, transform, unweight]) Series of projects in a number
specific factors
[Link]
Below is a summary of the main functions of the stat package.
models, to create different types of graphs.
a) Goodness of fit
Methods Description
[Link](data[, dist, distargs, a, . . . ]) Q-Q plot of the quantiles of x
in front of the quantiles/ppf of a
distribution
[Link](ax, line[, x, y, dist, fmt]) Draw a reference line for a
qqplot.
Q-Q Plot of the quantiles of two samples
samples.
[Link](data[, dist, fit, ...]) Class for construction
convenient Q-Q, P-P plots and
of probability.
b) Boxplots
Methods Description
violin plot for each data set in
the data sequence.
[Link](data[, ax, labels, . . . ])Graph bean of each dataset in the
data sequence.
c) Correlation graphics
Methods Description
correlation.plot_corr(dcorr[, xnames, . . . ]) Trace the correlation of many variables in
a fitted color grid.
Create a correlation plot grid.
plot_grids.scatter_ellipse(data[, level, . . . ]) Create a grid of graphs
dispersion with confidence ellipses.
d) Functional graphics
Methods Description
[Link](data[, ncomp, alpha, . . . ])Boxplot of the high density region
[Link](data[, xdata, labels, ...]) Functional graph.
[Link](data[, xdata, depth, . . . ]) Creates a rainbow plot for a
set of curves.
[Link](data[, method]) Calculate the band depth for
a set of functional curves.
e) Regression graphics
Methods Description
regressionplots.plot_fit(results, exog_idx) Regression fit graph
regressionplots.plot_regress_exog(results, . . . )Graph the regression results against a
regressor.
regressionplots.plot_partregress(endog, . . . ) Partial regression plot for a single
regressor
regressionplots.plot_partregress_grid(results) Partial regression plot for a
set of regressors.
regressionplots.plot_ccpr(results, exog_idx) CCPR against a regressor.
regressionplots.plot_ccpr_grid(results[, . . . ]) Generate graphs of the CCPR against a
set of regressors, graphs in
grid.
regressionplots.plot_ceres_residuals(. . . [, . . . ]) Produce a CERES (Conditional
Expectation Partial Residuals) for a
fitted regression model.
regressionplots.abline_plot([intercept, ... ]) Draw a line given an intercept and a
pending.
regressionplots.influence_plot(results[, . . . ]) Influence plot in regression.
regressionplots.plot_leverage_resid2(results) Leverage statistics of graphs.
Methods Description
tsaplots.plot_acf(x[, ax, lags, alpha, . . . ]) Draw the autocorrelation function
tsaplots.plot_pacf(x[, ax, lags, alpha, . . . ])Plot the partial autocorrelation function
tsaplots.month_plot(x[, dates, ylabel, ax]) Seasonal graph of monthly data
Quarterly seasonal plot of the data
g) Other graphics
Methods Description
factorplots.interaction_plot(x, trace, response) Interaction graph for
factor level statistics.
[Link](data[, index, ax, . . . ]) Create a mosaic chart from
from a contingency table.
mean_diff_plot(m1, m2[, sd_limit, ...]) Mean difference plot.
1. Clone the repository (folder where the package has been saved).
$ cd ~/Downloads
$ git clone [Link]
References
[1] H. [Link] Basics A Self-Teaching Introduction. Mercury, 2019.
David Jamieson Bolder. Credit-Risk Modelling: Theoretical Foundations, Diagnostic
Tools, Practical Examples, and Numerical Recipes in Python. Springer International
Publishing, 1st ed. edition, 2018.
Claus Fuher. Computing With Python: An Introduction to Python for Science &
Engineering. Pearson Education Limited, 2013.
Alex Galea. Applied Data Science with Python and Jupyter. Packt Publishing,
2018.
[5] Veena A. Gowrishankar [Link] to Python Programming. CRC, 2019.
[6] Magnus Lie [Link] Python: From Novice to Professional. Apress, 3rd
ed. edition, 2017.
Michael Heydt. Mastering pandas for Finance: Master pandas, an open source
Python Data Analysis Library, for financial data analysis. Packt Publishing, 2015.
Yves Hilpisch. Derivatives Analytics with Python: Data Analysis, Models, Simulation
tion, Calibration and Hedging. Wiley, 2015.
[9] Yves [Link] for Finance: Mastering Data-Driven Finance. O’Reilly Media,
2nd edition, 2019.
[10] Yves J [Link] for Finance: Analyze Big Financial Data. O’Reilly Media,
first edition edition, 2015.
[11] Pawel [Link] for Quants, volume 1. QuantAtRisk, 2015.
Eric Matthes. Python Crash Course A Hands-On, Project-Based Introduction to
Programming. No Starch Press, 2016.
[13] Bhaskar N. Das [Link] Python in 7 days : get up-and-running with Python.
Packt Publishing - ebooks Account, 2017.
[14] Fabio [Link] Data Analytics with Pandas, NumPy and Matplotlib [2nd ed.].
Apress, 2018.
[15] Ajay [Link] for R users : a data science approach. Wiley, 2018.
David J. Pine. Introduction to Python for Science and Engineering. Series in
Computational Physics. CRC Press, 2019.
Christopher Gardner Shayne Fletcher. Financial Modelling in Python (The Wiley
Finance Series). Wiley, 2009.
Ben Stephenson. The Python Workbook: A Brief Introduction with Exercises
Solutions. Texts in Computer Science. Springer, 2nd edition, August 2019.
[19] James Ma [Link] Python for Finance. Packt Publishing, 2015.
[20] Yuxing [Link] for Finance. Packt Publishing, 2nd edition, 2017.