Python Faker Module with Examples

Faker Module:

The Faker module is used to produce random data, including attributes such as name, age, and location.

The question now is, why would one require Fake data? We may require false or fake data to either fill in the blanks in the databases with artificial data or to just test an algorithm.

How to import the Faker module?

To examine the various functions and methods of the faker library, we must first import it.

Installation

pip install faker

Output:

Collecting faker Downloading Faker-10.0.0-py3-none-any.whl (1.2 MB) 
|████████████████████████████████| 1.2 MB 2.1 MB/s Requirement already 
satisfied: python-dateutil>=2.4 in /usr/local/lib/python3.7/dist-packages 
(from faker) (2.8.2) Requirement already satisfied: typing-extensions>=
3.10.0.2 in /usr/local/lib/python3.7/dist-packages (from faker) (3.10.0.2)
Requirement already satisfied: text-unidecode==1.3 in /usr/local/lib/
python3.7/dist-packages (from faker) (1.3) Requirement already satisfied:
six>=1.5 in /usr/local/lib/python3.7/dist-packages (from python-dateutil>=
2.4->faker) (1.15.0)
Installing collected packages:
faker Successfully installed faker-10.0.0

Importing

from faker import Faker

How to Create Fake data?

To generate some Fake data, we must first create a  faker object of the  Faker library and then apply various functions to the object to generate the false random data.

Use the faker.name() function to generate a random name.

Approach:

  • Import faker from Faker module using the import keyword.
  • Create an object for the Faker library and store it in a variable.
  • Generate a random name using faker_obj.name() function and print it.
  • The Exit of the Program.

Below is the implementation:

# Import faker from Faker module using the import keyword.
from faker import Faker
# Create an object for the Faker library and store it in a variable.
faker_obj = Faker()
# Generate a random name using faker_obj.name() function and print it
print(faker_obj.name())

Output:

Tammy Clay

Program to generate 8 random names:

# Import faker from Faker module using the import keyword.
from faker import Faker
# Create an object for the Faker library and store it in a variable.
faker_obj = Faker()
# Generate 8 random names using faker_obj.name() function, for loop and print it
for itr in range(8):
    print(faker_obj.name())

Output:

Bryan Hernandez 
Christopher Perez 
Phillip Freeman 
Edward Mills 
Larry Russell 
Denise Benjamin 
Mary Reeves 
Jesus Washington

How to Create Fake data in different languages?

By declaring it in the Faker object, we may easily generate fake data in multiple languages.

Approach:

  • Import faker from Faker module using the import keyword.
  • Create an object for the Faker library by passing some random language and store it in a variable.
  • Generate 8 random names(in hindi) using faker_obj.name() function, for loop and print it.
  • The Exit of the Program.

Below is the implementation:

# Import faker from Faker module using the import keyword.
from faker import Faker
# Create an object for the Faker library by passing some random
# language as an argument to it and store it in a variable.
faker_obj = Faker('hi_IN')
# Generate 8 random names using faker_obj.name() function, for loop and print it.
for itr in range(8):
    print(faker_obj.name())

Output:

सिंह, रतन
विवेक सेनाधीश 
मोहिनी नाम 
शान्ता बालासुब्रमणियम 
अद्विका मदन 
दीया रामशर्मा 
हेगडे, मोहिनी
शनाया मंडल

Generation of Faker Text

Use the text() function, to generate fake text and sentences with the same faker objects

Approach:

  • Import faker from Faker module using the import keyword.
  • Create an object for the Faker library and store it in a variable.
  • Generate fake text using the text() function with the faker object and print it.
  • The Exit of the Program.

Below is the implementation:

# Import faker from Faker module using the import keyword.
from faker import Faker
# Create an object for the Faker library and store it in a variable.
faker_obj = Faker()
# Generate fake text using the text() function with the faker object and print it.
print(faker_obj.text())

Output:

Citizen spend wear reach customer science. Charge sell instead from certain. Increase work free teacher partner weight.

Generation of Fake Tabular data

Let’s now try to generate a large number of data points in Python using the dataframes of pandas library. We use the profile() method of the faker object to collect various or multiple types of data.

# Import pandas library as pd using the import keyword.
import pandas as pd
# Import faker from Faker module using the import keyword.
from faker import Faker
# Create an object for the Faker library and store it in a variable.
faker_obj = Faker()
# Use profile() method of the faker object to collect various or multiple
# types of data.
# Store it in another variable.
rslt_data = [faker_obj.profile() for a in range(5)]
# Generate some random tabular data using the DataFrame() function
# of pandas library
# Store it in another variable.
tab_data = pd.DataFrame(rslt_data)
# Print the tabular fake data
print(tab_data)

Output:

                           job  ...   birthdate
0  Research scientist (medical)  ...  1924-01-28
1                Therapist, art  ...  1908-02-22
2                  Tour manager  ...  1932-08-31
3   Careers information officer  ...  1995-06-03
4         Nutritional therapist  ...  2015-02-20

[5 rows x 13 columns]