#5.3 Find companies that lost or gained from the COVID19 pandemic (Python Financial Analysis)

wsh
Python Financial Analysis
7 min readAug 13, 2021

Python financial Analysis (PFA5P3)

Python Financial Analysis | Home

Introduction

If you want to do financial analysis in Python, measuring performance of specific companies is one of the most basic things. Over the previous stories, we saw how to analyze the performance of each sector or market cap class. The next is about specific companies, using COVID19 pandemic as an example.

The code and dataset are available below.

You can download the data set from this link
https://drive.google.com/drive/folders/1Ux2u1s5mctYiywS08sv7_3_PbnWd8v0G?usp=sharing

The full source code is available from here
https://drive.google.com/drive/folders/1F5n6a6hY9g_GLhPV80DCrAypa2onj0c1?usp=sharing

List of articles

1. Python Financial Analysis

1 Read fundamental data from a CSV in Python
2 Handling table like data in Python with DataFrame
3 Make graphs of stock price in Python
4.1 Make custom market index — prerequisites
4.2 Make custom market index — make your own index
4.3 Make custom market index — market cap based index
5.1 Analyze COVID-19 Impacts by Sector in Python — compare weighted average prices
5.2 Analyze COVID-19 Impacts by Market Caps in Python — compare weighted average prices
5.3 Find companies that lost or gained from the COVID19 pandemic

2. Python Data Analysis Basics (easiest ways)

Python “datetime” in the easiest way (how to handle dates in data science with Python)
Python DataFrame slicing in the easiest way (How to find a company from 5000 companies)

Performance Analysis

1. Earlier stage of pandemic (Jan 2020 — May 2020)

The image below shows the performance of most featured 10 companies during the earlier stage of pandemic. It takes relative price change of these companies from Jan 2, 2020 to Mar 20, 2020.

We first pick up the largest 100 companies (largest market caps) from the about 5000 companies, and then sort them by their relative price changes. The 10 companies shown on the line chart are selected from the 5 most growing companies and from the 5 most loosing companies.

As you may already know, Zoom (ZM) is the largest winner during the beginning of pandemic. Its price got 89% growth until the end of this period. And, from the change the price, you can also see that investors started to notice that Zoom would grow at late January. After that, the price got a steep gain.

Tesla (TSLA) is also growing, but this is not only during the pandemic. Its price growth started before the pandemic emerged.

While a tech company was getting a huge gain, Boeing (BA) had a sever drop of as much as 72%. If you read the last story, you can see that this phenomenon is not just of Boeing. Most companies that belong to “Aerospace” sectors encountered the similar problem. This is because people got stuck in home and could not enjoy travelling.

Another thing we notice from the list of 100 companies, sectors like “Computer and Technology”, “Retails Wholesale”, and “Medical” come top on the table, while sectors like “Oils Energy”, “Aerospace”, and “Finance” are at bottom on the table. This reflects the difference between winner sectors and loser sectors during the earlier stage of pandemic. You can see more details from this story:
#5.1 Analyze COVID-19 Impacts by Sector in Python — compare weighted average prices (Python Financial Analysis)

1. Late of pandemic (Jan 2020 — Apr 2021)

Performances until late of the pandemic has much more clear difference. The fist thing is that Tesla got a huge gain during this period despite of its closed factories decrease of demand. This is mainly due to a lot of expectations of future growth.

Zoom’s growth didn’t last for long. From Oct 2020, the company lost almost one third of its growth gained before this start of drop.

While 84 companies of the largest 100 companies got positive growth until Apr 2021, the other 16 companies are still suffering from the pandemic. For example, Raytheon (RTX) is still 52% of its pre-pandemic value, Boeing (BA) is 70%, and AT&T (T) is 85%.

The last thing we notice is that the sector “Medical” does not come at the top of the table! At the begging of the pandemic, investors thought this sector will grow due to expected huge demands, but the expectation didn’t last.

Analysis in Python

1. Import packages and read dataset

As usual, we use the three packages “NumPy”, “Pandas”, and “Matplotlib”. If you don’t’ know how to install packages, visit this story.
#1 Read fundamental data from a CSV in Python (Python Financial Analysis)

The next is reading the dataset of meta and price data. The dataset can be downloaded from here (Google Drive). From the drive, download the CSV files “meta.csv” and “pricedata_reshaped.csv”. Then run the “read_csv()” function of Pandas. The output is DataFrame. If you don’t know so much about DataFrame and its usage, visit this story:
#2 Handling table like data in Python with DataFrame (Python Financial Analysis)

2. Convert strings into datetime objects

In this story, we handle price data, and these price data are time series data. This means, we must handle dates and times in programming. The most standard form to specify date in Python is “datetime” objects. Because the CSV file saves date in text format like “2020–01–02”, we have to convert them into Python “datetime” objects. The code below does that for you. If you are not familiar with “datetime”, this story will help you:
Python “datetime” in the easiest way (how to handle dates in data science with Python)

3. Remove missing values “NaN”

If a value in a dataset is missing, it’s quite common that a special value “NaN” is inserted instead. This is for making it easy to deal with missing values in Python. For example, a company that didn’t exist few years ago has NaN value as its prices at that time.

The problem is any calculation that includes NaN values will result in NaN values. Let’s multiple NaN to 0. The result is not 0. You can check it with “np.nan * 0”.

Thus, if we want to make calculations that include price data, we msut replace NaN values with zeros. The code below does that for us.

4. Defined date range and crop the data table

The range of data used for performance analysis is either “2020–01–02” to “2020–03–20” or “2020–01–02” to “2021–04–20”. We crop the DataFrame “price” according to the range, and define a label “CROP_LABEL” for making file name of CSV files and image names. We’ll use it later.

We must make another crop on the DataFrame “meta”. Because we don’t want to know performance of the 5000 companies, we pick up the largest 100 companies based on their market caps. This can be done by sorting the DataFrame firsts in descending order, and then select the first 100 rows from the table.

5. Calculate price change

The price change is calculated as relative change from the biggening of the period.

We first add a new column to “meta_top100” as a save location of performances, and sort the table of price “price_crop” in a way that the oldest prices come at the first row. s

Inside the iteration of “for” loop over the 100 tickers, we get the price of a company and divide it with its latest price. We must pay attention here to the possibility that “price_before” could be 0 because we replaced NaN values with zeros. We make sure that with a “if” statement. We can’t divide a number with 0.

The result is save to the new column “pandemic_pricechange” of “meta_top100”. To access a specific entry of the column, we give the DataFrame a condtion “[meta_top100[“ticker”] == ticker]”.

6. Save the result as CSV

Before making a line chart, we save the data to a CSV file. Because we want to make the CSV look better, we sort “meta_top100” by the performance “pandemic_pricechange”, and round its values down to 3 decimals.

As you may know, “meta_top100” has a lot columns. Thus, we select just necessary columns “ticker”, “name”, “sector” and “pandemic_pricechange” as the targets for saving. Then we invoke the “to_csv()” function, specifying that we don’t need the index to be saved. The result is the table showen above.

7. Line chart of featured 10 companies

Finally, we display the history of price change of most featured 10 companies. Five of them are companies that gained most during the pandemic. And the other five companies are the ones that lost most. The tickers of them are stored in “TICKERS_FEATURED”.

Inside the “for” loop, we calculate the relative price change, and add a line chart on the figure. The price at the beginning of range is “price_crop[ticker].iloc[0]”.

After adding 10 line charts, we save the figure. The result is the image show above.

Full Python code

Related stories

#5.1 Analyze COVID-19 Impacts by Sector in Python — compare weighted average prices (Python Financial Analysis)

#5.2 Analyze COVID-19 Impacts by Market Caps in Python — compare weighted average prices (Python Financial Analysis)

Other Links

Python Financial Analysis | Home
Python Data Analysis | Home

--

--