Four Exciting Python Modules That Everyone Should Know
Before we jump into this week's article, let me ask you something:
Are you tired of spending hours on data cleaning and wrangling before diving into your analyses with Python?
Leveraging the latest data stack frees up valuable time for conducting Python analyses that yield data-rich business insights. Graphext is an exploratory data analysis platform that enhances your Python code in the following ways:
Effortless Data Cleaning: Preview your variable distribution and summary statistics within Graphext, saving time on data cleaning by identifying the variables that need your attention beforehand.
Efficient Feature Engineering: Interactively identify essential features on the platform to enhance your Python model's performance.
Intuitive Data Visualization: Gain insights into data patterns and preview visualizations. Utilize Matplotlib and Seaborn to craft engaging narratives that stand out!
Enhance your Python workflow today with Graphext. Try it for FREE! Click below:
Introduction
One of the most exciting things about Python is the number of external libraries or modules that are available for us to use. External modules significantly expand Python's capabilities by offering a vast array of additional functionality. Whether it's web development, data analysis, machine learning, or any other domain, there's likely an external module to help. In this article, we are going to explore four exciting external modules and their capabilities that you can use in your Python.
Currency Converter
In Python, with just a few lines of code, we can write a program that converts one currency into another using up-to-date exchange rates or historical rates. First, install the forex library using pip:
pip install forex-python
The forex-python module simplifies currency-related operations and is a valuable resource for financial professionals, developers, and businesses dealing with international transactions and foreign exchange markets. It empowers users with access to real-time and historical exchange rate data. Let's say you want a historical exchange rate of USD to EUR on 2020-10-23. We can use the module to get the exchange rate.
We can also use this module to convert one currency to another using real-time rates. All we need are the currency codes of the currencies we want to convert. For example, if, let's say, you want to convert $500 to EUR using real-time rates, here is how you can do it using this module:
Dummy Data Generator
We can generate fake data using Python. Python has a library called Faker that generates different types of data. This module can generate various types of fake data, including names, addresses, phone numbers, email addresses, dates, and more. It can also produce random text, such as paragraphs or sentences. First, install Faker using:
pip install Faker
Let’s say we want to generate a random list of 10 Italian names and addresses. First, we instantiate the Faker object. This object allows us to localize the data that we want to generate by passing a locale as an argument. Since we want Italian names and addresses, we are going to pass 'it_IT' as the argument: Here is the code below:
We can also use this module to generate fake texts. By default, if we do not pass a locale as an argument, the Faker will use 'en_US' and generate text in English. See below:
Please note that the data generated by this module is commonly used in software testing. It helps ensure that applications work correctly with a variety of data types and values.
Censor Profanity
If you have bad words in your text that you want to censor, you can use the better_profanity library. First install using pip:
pip install better_profanity
This module provides a simple and effective way to filter and censor profane language from text. It's particularly useful for content moderation, chat applications, and any situation where you need to ensure that user-generated content remains free of offensive or inappropriate language. Below, we have a string that has a bad word; we are going to pass the string to the module to have the bad word censored.
By default, it uses the asterisk (*) to censor bad words. However, we can also set the censor character. See the example below. We set $ as the set character.
We can also create a custom list of words we want to censor. If a text contains a word from the list, then it will be censored. See the example below:
If we just want to check if a string contains profanity, we can use the contains_profanity() method, which will return a Boolean value of True if it contains profanity and False if it has no profanity.
Download Financial Data
What if we want to extract financial data from online sources? Is there a module that you can use? Yes, there is one. yfinance provides a convenient way to download historical market data, such as stock prices and financial statements. It serves as a convenient tool for data analysts, financial professionals, and researchers who need to access a wide range of financial information for analysis, modeling, and research. You can install yfinance using pip:
pip install yfinance
Here's an example of how you may use yfinance to fetch the historical stock price of Apple using the stock symbol "AAPL":
We can also use yfinance to fetch the financial statements of companies. Let's say we want to fetch the income statements of Microsoft for 2020. We pass the Microsoft stock symbol to the Tinker() module. We use the financials attribute to extract the income statements. Here is the code below:
Please note that the data obtained from Yahoo Finance is for personal use only.
Conclusion
These are just a few libraries that you can use in your applications. Use forex-python if you want to obtain exchange rates or convert currencies. You can use the Faker module if you want to generate dummy data to test your applications. If you want to censor texts, then you can use the better_profanity module. And if you are looking for financial data to use in your analysis, you can turn to the yfinance module. Thank you for reading. You can download the code in this article from GitHub. Please like, share, and subscribe to this newsletter if you are not yet a subscriber.
Python Question of the Week:
What is the output of this code, and why? Share your answers in the comment section.