- Notebook Author: Trenton McKinney
- Course:
**DataCamp: Introduction to Python for Finance**- This notebook was created as a reproducible reference.
- The material is from the course
- I completed the exercises
- If you find the content beneficial, consider a DataCamp Subscription.

- Most of the course material is very basic so has not been included in the Notebook
- Great course for those with little or not Python experience

In [1]:

```
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from numpy import NaN
from glob import glob
import re
```

In [2]:

```
pd.set_option('max_columns', 200)
pd.set_option('max_rows', 300)
pd.set_option('display.expand_frame_repr', True)
```

- Most data files for the exercises can be found on the course site
- Other data files may be found in my DataCamp repository

In [3]:

```
stocks1 = 'data/intro_to_python_for_finance/stock_data.csv'
stocks2 = 'data/intro_to_python_for_finance/stock_data2.csv'
sector_100 = 'data/intro_to_python_for_finance/sector.txt'
exercises = 'data/intro_to_python_for_finance/exercise_data.csv'
```

*Course Description*

The financial industry is increasingly adopting Python for general-purpose programming and quantitative analysis, ranging from understanding trading dynamics to risk management systems. This course focuses specifically on introducing Python for financial analysis. Using practical examples, you will learn the fundamentals of Python data structures such as lists and arrays and learn powerful ways to store and manipulate financial data to identify trends.

This chapter is an introduction to basics in Python, including how to name variables and various data types in Python.

Using variables to evaluate stock trends

$\text{Price to earning ratio} = \frac{\text{Maket Price}}{\text{Earnings per share}}$

In [4]:

```
price = 200
earnings = 5
pe_ratio = price/earnings
pe_ratio
```

Out[4]:

Booleans are used to represent True or False statements in Python. Boolean comparisons include:

Operators | Descriptions |
---|---|

> | greater than |

>= | greater than or equal |

< | less than |

<= | less than or equal |

== | equal (compare) |

!= | does not equal |

This chapter introduces lists in Python and how they can be used to work with data.

Methods | Functions |
---|---|

All methods are functions | Not all functions are methods |

List methods are a subset of built in functions in Python | |

Used on an object | Requires an input of an object |

prices.sort() | type(prices) |

- Functions take objects as inputs or are "passed" an object
- Methods act on objects

This chapter introduces packages in Python, specifically the NumPy package and how it can be efficiently used to manipulate arrays.

*Why use an array for financial analysis?*

- Arrays can handle very large datasets efficiently
- Computationally memory efficient
- Faster calculations and analysis than lists
- Diverse functionality (many functions in Python packages)

- All dtypes in a numpy array are the same
- Each element of a python list keeps its dtype

In [5]:

```
# Arrays - element-wise sum
array_A = np.array([1, 2, 3])
array_B = np.array([4, 5, 6])
array_A + array_B
```

Out[5]:

In [6]:

```
# Lists - list concatenation
list_A = [1, 2, 3]
list_B = [4, 5, 6]
list_A + list_B
```

Out[6]:

In this chapter, you will be introduced to the Matplotlib package for creating line plots, scatter plots, and histograms.

In [7]:

```
df = pd.read_csv(stocks1)
df.head()
```

Out[7]:

In [8]:

```
plt.plot(df.Day, df.Price, color='red', linestyle='--')
# Add x and y labels
plt.xlabel('Days')
plt.ylabel('Prices, $')
# Add plot title
plt.title('Company Stock Prices Over Time')
```

Out[8]:

In [9]:

```
df = pd.read_csv(stocks2)
df.head()
```

Out[9]:

In [10]:

```
# Plot two lines of varying colors
plt.plot(df.day, df.company1, color='red')
plt.plot(df.day, df.company2, color='green')
# Add labels
plt.xlabel('Days')
plt.ylabel('Prices, $')
plt.title('Stock Prices Over Time')
```

Out[10]:

In [11]:

```
df[['company1', 'company2']].plot()
```

Out[11]:

In [12]:

```
plt.scatter(df.day, df.company1, color='green', s=0.1)
```

Out[12]:

- Tell the distribution of the data
- Uses in Finance
- Economic Indicators
- Stock Returns
- Commodity Prices

- Is you data skewed?
- Is you data centered around the average?
- Do you have any abnormal data points (outliers) in your data?

```
import matplotlib.pyplot as plt
plt.hist(x=prices, bins=3)
plt.show()
```

```
import matplotlib.pyplot as plt
plt.hist(x=prices, bins=6, density=True)
plt.show()
```

- At times it's beneficial to know the relative frequency or the percentage of observations (rather than frequency counts)

```
plt.hist(x=prices, bins=6, density=True)
plt.hist(x=prices2, bins=6, density=True)
plt.show()
```

```
plt.hist(x=prices, bins=6, density=True, alpha=0.5)
plt.hist(x=prices2, bins=6, density=True, alpha=0.5)
plt.show()
```

```
plt.hist(x=prices, bins=6, density=True, alpha=0.5, label='Prices 1')
plt.hist(x=prices2, bins=6, density=True, alpha=0.5, label='Prices New')
plt.legend()
plt.show()
```

In [13]:

```
plt.hist(df.company2, bins=100, ec='black')
plt.show()
```

In [14]:

```
df_exercises = pd.read_csv(exercises)
df_exercises.head()
```

Out[14]:

In [15]:

```
df_exercises.hist(bins=100, alpha=0.4, ec='black')
plt.show()
```

In [16]:

```
# Plot histogram of stocks_A
plt.hist(df_exercises.stock_A, bins=100, alpha=0.4, label='Stock A')
# Plot histogram of stocks_B
plt.hist(df_exercises.stock_B, bins=100, alpha=0.4, label='Stock B')
# Add the legend
plt.legend()
# Display plot
plt.show()
```

In this chapter, you will get a chance to apply all the techniques you learned in the course on the S&P 100 data.

- Python shell and scripts
- Variables and data types
- Lists
- Arrays
- Methods and functions
- Indexing and subsetting
- Matplotlib

*Standard and Poor's S&P 100:*

- made up of major companies that span multiple industry groups
- used to measure stock performance of large companies

- EPS: earning per share

In [17]:

```
df = pd.read_csv(sector_100)
df.head()
```

Out[17]:

In [18]:

```
df.tail()
```

Out[18]:

$\text{Price to earning ratio} = \frac{\text{Maket Price}}{\text{Earnings per share}}$

- The dollar amount one can expect to invest in a company in order to receive one dollar of the company's earnings
- The ratio for valuing a company that measures its current share price relative to the per-share earnings
- In general, higher P/E ratio idicates higher growth expectations

*Given*

- List of data describing the S&P 100: names, prices, earnigns, sectors

*Objective Part I*

- Explore and analyze the S&P 100 data, specifically the P/E ratios of S&P 100 companies

- Step 1: examine the lists
- Step 2: Convert lists to arrays
- Step 3: Elementwise array operations

In [19]:

```
names = df.Name.values
prices = df.Price.values
earnings = df.EPS.values
sectors = df.Sector.values
```

In [20]:

```
type(names)
```

Out[20]:

Stocks in the S&P 100 are selected to represent sector balance and market capitalization. To begin, let's take a look at what data we have associated with S&P companies.

Four ** lists**,

*Instructions*

- Print the first four items in
.*names* - Print the name, price, earning, and sector associated with the last company in the lists.

In [21]:

```
# First four items of names
print(names[:4])
# Print information on last company
print(names[-1])
print(prices[-1])
print(earnings[-1])
print(sectors[-1])
```

NumPy is a scientific computing package in Python that helps you to work with arrays. Let's use array operations to calculate price to earning ratios of the S&P 100 stocks.

The S&P 100 data is available as the lists: ** prices** (stock prices per share) and

*Instructions*

- Import the
as*numpy*.*np* - Convert the
and*prices*lists to arrays,*earnings*and*prices_array*, respectively.*earnings_array* - Calculate the price to earnings ratio as
.*pe*

```
# Convert lists to arrays
prices_array = np.array(prices)
earnings_array = np.array(earnings)
```

In [22]:

```
# Calculate P/E ratio
pe = prices/earnings
pe[:10]
```

Out[22]:

*Given*

- Numpy arrays of data describing the S&P 100: names, prices, earnings, sectors

*Objective Part II*

- Explore and analyze sector-specific P/E ratios within companies of the S&P 100

- Step 1: Create a boolean filtering array
- Step 2: Apply filtering array to subset another array
- Step 3: Summarize P/E ratios
- Calculate the average and standard deviation of these sector-specific P/E ratios

In this lesson, you will focus on two sectors:

- Information Technology
- Consumer Staples

** numpy** is imported as

*Instructions 1/2*

- Create a boolean array to determine which elements in
are*sectors*.*'Information Technology'* - Use the boolean array to subset
and*names*in the Information Technology sector.*pe*

In [23]:

```
# Create boolean array
boolean_array = (sectors == 'Information Technology')
# Subset sector-specific data
it_names = names[boolean_array]
it_pe = pe[boolean_array]
# Display sector names
print(it_names)
print(it_pe)
```

*Instructions 2/2*

- Create a boolean array to determine which elements in
are*sectors*.*'Consumer Staples'* - Use the boolean array to subset
and*names*in the Consumer Staples sector.*pe*

In [24]:

```
# Create boolean array
boolean_array = (sectors == 'Consumer Staples')
# Subset sector-specific data
cs_names = names[boolean_array]
cs_pe = pe[boolean_array]
# Display sector names
print(cs_names)
print(cs_pe)
```

In this exercise, you will calculate the mean and standard deviation of P/E ratios for Information Technology and Consumer Staples sectors. ** numpy** is imported as

*Instructions 1/2*

Calculate the mean and standard deviation of the P/E ratios (** it_pe**) for the Industrial Technology sector.

In [25]:

```
# Calculate mean and standard deviation
it_pe_mean = np.mean(it_pe)
it_pe_std = np.std(it_pe)
print(it_pe_mean)
print(it_pe_std)
```

*Instructions 2/2*

- Calculate the mean and standard deviation of the P/E ratios (
) for the Consumer Staples sector.*cs_pe*

In [26]:

```
# Calculate mean and standard deviation
cs_pe_mean = np.mean(cs_pe)
cs_pe_std = np.std(cs_pe)
print(cs_pe_mean)
print(cs_pe_std)
```

Let's take a closer look at the P/E ratios using a scatter plot for each company in these two sectors.

The arrays ** it_pe** and

*Instructions*

- Draw a scatter plot of
ratios with red markers and*it_pe*label.*'IT'* - On the same plot, add the
ratios with green markers and*cs_pe*label.*'CS'* - Add a legend to this plot.
- Display the plot.

In [27]:

```
it_id = np.arange(0, 15)
cs_id = np.arange(0, 12)
# Make a scatterplot
plt.scatter(it_id, it_pe, color='red', label='IT')
plt.scatter(cs_id, cs_pe, color='green', label='CS')
# Add legend
plt.legend()
# Add labels
plt.xlabel('Company ID')
plt.ylabel('P/E Ratio')
plt.show()
```

*Notice that there is one company in the IT sector with an unusually high P/E ratio*

- Step 1: Make a histogram of the P/E ratios
- Step 2:
- Identify the outlier P/E ratio
- Create a boolean array filter to subset this company
- Filter out this company information from the provided datasets

To visualize and understand the distribution of the P/E ratios in the IT sector, you can use a histogram.

The array ** it_pe** from the previous exercise is available in your workspace.

*Instructions*

- Selectively import the
module of*pyplot*as*matplotlib*.*plt* - Plot a histogram of
with 8 bins.*it_pe* - Add the x-label as
and y-label as*'P/E ratio'*.*'Frequency'* - Display the plot.

In [28]:

```
# Plot histogram
plt.hist(it_pe, bins=8, ec='black')
# Add x-label
plt.xlabel('P/E ratio')
# Add y-label
plt.ylabel('Frequency')
# Show plot
plt.show()
```

- Histograms can help you to identify outliers or abnormal data points. Which P/E ratio in this histogram is an example of an outlier?

*A stock with P/E ratio > 50.*

You've identified that a company in the Industrial Technology sector has a P/E ratio of greater than 50. Let's identify this company.

** numpy** is imported as

Instructions

- Identify the P/E ratio greater than 50 and assign it to
.*outlier_price* - Identify the company with P/E ratio greater than 50 and assign it to
.*outlier_name*

In [29]:

```
# Identify P/E ratio within it_pe that is > 50
outlier_price = it_pe[it_pe > 50]
# Identify the company with PE ratio > 50
outlier_name = it_names[it_pe == outlier_price]
# Display results
print(f'In 2017 {outlier_name[0]} had an abnormally high P/E ratio of {round(outlier_price[0], 2)}.')
```