Hey there! Today we are going to learn about the Faker module which is going to be very interesting to learn. So let’s begin!
Introduction to the faker module
Faker module is used to generate random data including random attributes like name, age, location, etc.
Now the question is why would one need Fake data? We might need fake data to either fill the missing values in the databases with some dummy data or to simply test an algorithm.
Importing the faker module
To explore different functions and methods of faker library, we first have to import it.
In case the import faker library throws an error, we will install the library by using the
We can import the library we have to use the code given below.
from faker import Faker
Creating fake data
To create some Fake data, we need to first create a
faker object of the Faker library and apply various functions on the object to get the fake random data.
The code below prints a random name using
faker = Faker() print(faker.name())
To make it more understandable let’s print 5 random names using the same function. The code and output for the same is shown below.
faker = Faker() for i in range(5): print(faker.name())
The output of the code above is displayed below.
Stephanie Rivera Gina Morgan Elizabeth Garcia Joanne Collier Jessica Berry
Creating fake data in a different language
We can also generate fake data in different languages by defining it in the
Faker object created. Let’s generate some data in Hindi language. The code for the same is shown below.
faker1 = Faker('hi_IN') for i in range(5): print(faker1.name())
faker1 object that we have declared here is in Hindi Language. So if this object is created to print dummy names then the result looks something like what’s shown below.
पाटिल, इशान लाला, जयदेव ऐश्वर्या लाला ललित गणेश चेतना मल्लिक
Generating fake text
The same objects can be used to generate text and sentences with the help of the
text function. The code for the same is shown below.
faker = Faker() print(faker.text())
The output of the same is shown below.
Big thought American. Per red plan hundred language test. Language early bill citizen total if officer because. Example practice other street newspaper kid level.
Generate fake tabular data
Let’s now try to create a whole bunch of data points in the form of
pandas library in python. To collect multiple types of data we make use of the
profile function of the faker object created.
The code for the same is shown below.
import pandas as pd faker = Faker() data = [faker.profile() for i in range(10)] df = pd.DataFrame(data)
On printing the first five data points of the dataframe created. We get the results which are displayed below.
In this tutorial, we saw how we can use Faker library to generate fake data in not only English language but also in different languages.
The same module can be used to generate full datasets and then use the datasets for multiple purposes including applying the model to ML models.
Thank you for reading.