Install required libraries
- Install the requests library to make HTTP Requests
- Install pandas to do some data exploratory
-
In this recipe we will use altair to do the data visualization, if you are unfamiliar with altair, try to watch this playlist Altair Tutorial
Accessing List of All Subsectors
Before writing your code, you must access your API Keys in the Sectors Financial API Page. We will use that API Key later as an authorization for the API.data_all_subsectors
you’ll see a list that look like this:
Data Processing
Transforming data to Pandas DataFrame
Before doing data visualization, let’s use pandas to perform a little bit of data processing. Begin by converting the list of subsectors to a pandas DataFrame. Since we are going to use only the subsectors data, we can drop the sectors data after making it into a dataframe.subsectors | |
---|---|
0 | telecommunication |
1 | oil-gas-coal |
2 | financing-service |
3 | investment-service |
4 | apparel-luxury-goods |
Processing the data
As you can see, the subsector’s naming standard is not user-friendly, let’s fix that too before doing the visualization, you just need to add two lines of code to make it more readable!subsectors | |
---|---|
0 | Telecommunication |
1 | Oil Gas Coal |
2 | Financing Service |
3 | Investment Service |
4 | Apparel Luxury Goods |
Data Visualization
Now let’s try to visualize all the subsectors using altair!Let’s look at another API
The list of all subSectors Financial API only returns a list of subsectors. This is not terribly insightful, so let’s shift our focus onto another Sectors Financial API that lists all companies within each subsector. That will give us more data to work with and lay the foundation for our data visualization later.Collect the data
We will join the data from both API to a single list using the code below:Total Companies by Subsector
In this recipe, let’s visualize Total Companies by Subsector using the data that we already have. The first step is to clean our data using the same way and group the data by subsector so we can get the count of companies by each subsector.subsectors | total_companies | |
---|---|---|
0 | Alternative Energy | 2 |
1 | Apparel Luxury Goods | 23 |
2 | Automobiles Components | 17 |
3 | Banks | 47 |
4 | Basic Materials | 108 |
Let’s visualize it!
Now let’s do the most basic visualization using altair, beginning with a bar chart to see the number of companies in each subsector.
Top 5 Biggest Subsector by Company
A Better visualization!
