List of All Subsectors
By: Aidityas Adhakim · April 16, 2024
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.
If you try to print the 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.
The results will look like the following:
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!
After the data processing, now it should look 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:
The code above basically retrieves all the list of companies from all subsectors. You should be able to see the output like below:
Now since we already join the data let’s do some data processing and visualize the data.
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.
The code above will make the subsectors more readable and group the data by subsectors and get the total companies on each subsector. Run this code below to see the output.
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.
Well now we have done a very basic bar chart visualization using Altair, but it looks really long and messy! Let’s reduce the number to be top 5 biggest subsector by company and do some adjustment on the visualization.
Top 5 Biggest Subsector by Company
A Better visualization!
And now we have a much better visualization we can show our boss! 😉
In this recipe, we’ve pulled the data from Sectors and do a little bit data processing and some simple data visualization! Now you can take a look at another API and try to build your own visualization based on your needs or you can simply follow along the rest of this cookbook to see more recipes that you can try!