Create Mini Project Using Pandas — The Financial Information Grabber
These days, the US market is facing the highest inflation in 30 years. This inflation happened because of the market's higher demand than the supply. It makes the fed add the interest rate to decrease the inflation speed. After the fed turned the interest rate, every market seemed to be crashed.
By 2022, the crypto market is going more than 50%, and the NASDAQ index was also going down significantly. At this time, better we have the cash to be ready to join the party when the market recovers. Let's create some code to improve our skills to fill the blank time.
Pandas is a powerful tool library of Python, and you could use it to process tabular data. Not only that, but it also could do some scrapping stuff. In this project, I would like to share a very simple program to grab every country's economic condition. Okay, let's do it.
The concept is everything; this is the workflow of this project like this.
We will collect information from the source and parse it into a data frame. Next, our program will process the information to give us insight into the economic condition of every country. After we get the information, we could use it for our financial strategy. Is it easy, right? Let's do it.
Below is the code section. I will try to explain it easily in steps.
- Import the pandas library
2. Scrape the data; because the source is a tabular formatted, we could easily scrape the data and then read it using
The output will be like this.
Why do we use
? Because the data frame is saved as a list, you could simply read the 0 indexes to access it. Below is the actual format of scrapped data (without
3. Since we want to collect information based on the country, we select corresponding columns based on a row value (e.g. country). It could be quickly done using
loc. For example, if we want to get the information about the US, you could efficiently run this program.
You will get the output like this.
4. Because this program's purpose wants to create a CLI program, pandas are not great at it, so let's iterate the output to make it more CLI-able.
From the code above, you could easily understand that we are getting the column name and then adding the information.
Also, if you prefer one-line code, you could make the function like this.
The output is like this.
5. Finally, let's create the function from it, so the user could just input the country data, and the data will be collected and then displayed to him.
Simple, right? When the code is run, you only need to put a named country on it, which will display the country's financial information. But is it done? Not yet.
6. The code still has a bug; if the country you input doesn't exist, it will be an error. To prevent it, I added some lines to check whether the country exists or not. If it exists, it will display the information. If not, it will inform you that the country doesn't exist.
Simple, right, but I believe there will be a new question, how to create country.txt? It was effortless. You only need to save all information from column country into country.txt.
The text is used to help others find the spelling name of the country they desire to see. It will look like this.
Here's the simulation when I run the code
Congratulations, in this story, you already create a mini project using Pandas, the financial information grabber. Still, there is much room you could improve, such as you want to create a dashboard from the pandas data frame, creating the GUI information, etc. It's up to you to improve it or not.
Here's the full version of the source code that I used.
Thanks for reading.