How to Create Fake Data with Faker | by Khuyen Tran | Towards Data Science

Let’s say you want to create data with certain data types (bool, float, text, integers) with special characteristics (names, address, color, email, phone number, location) to test some Python libraries or specific implementation. But it takes time to find that specific kind of data. You wonder: is there a quick way that you can create your own data?
What if there is a package that enables you to create fake data in one line of code such as this:
fake.profile()
This can be done with Faker, a Python package that generates fake data for you, ranging from a specific data type to specific characteristics of that data, and the origin or language of the data. Let’s discover how we can use Faker to create fake data.
Start with installing the package
pip install Faker
Import Faker
from faker import Fakerfake = Faker()
Some basic methods of Faker:
>>> fake.color_name()'SeaGreen'>>> fake.name()'Vanessa Schroeder'>>> fake.address()'3138 Jennings Shore\nPort Anthony, MT 90833'>>> fake.job()'Buyer, industrial'>>> fake.date_of_birth(minimum_age=30)datetime.date(1906, 9, 18)>>> fake.city()'Rebeccastad'
Let’s say you are an author of a fiction book who want to create a character but find it difficult and time-consuming to come up with a realistic name and information. You can write
>>> name = fake.name()>>> color = fake.color_name()>>> city = fake.city()>>> job = fake.job()>>> print('Her name is {}. She lives in {}. Her favorite color is {}. She works as a {}'.format(name, city,color, job))
Outcome:
Her name is Natalie Gamble. She lives in East Tammyborough. Her favorite color is Magenta. She works as a Metallurgist
With Faker, you can generate a persuasive example instantly!
Luckily, we can also specify the location of the data we want to fake. Maybe the character you want to create is from Italy. You also want to create instances of her friends. Since you are from the US, it is difficult for you to generate relevant information to that location. That can be easily taken care of by adding location parameter in the class Faker
fake = Faker('it_IT')for _ in range(10):   print(fake.name())
Outcome:
Lando Bertoli-Bulzoni Danilo Gianvecchio Dott. Margherita Veneziano Bianca Morabito  Alfredo Rossetti Claudia Chechi Dott. Gastone Loredan  Dott. Fulvio Russo Camilla Crisafulli-Gentileschi Agnolo Gioberti
Or create information from multiple locations
fake = Faker(['ja_JP','zh_CN','es_ES','en_US','fr_FR'])for _ in range(10):  print(fake.city())
Outcome:
Ceuta Juanhaven 佳市 East Sarah 山武郡横芝光町 川崎市宮前区 Blondel-sur-Pottier West Christine Lake Amandahaven Weekshaven
If you are from these specific countries, I hope you recognize the location. In case you are curious about other locations that you can specify, check out the doc here.
We can create random text with
>>> fake.text()'Lay industry reach move candidate from local spring. Wind someone really but. One rock fund different down own.'
Try with the Japanese language
>>> fake = Faker('ja_JP')>>> fake.text()'普通の器官証言する仕上げ鉱山癌。カラム索引障害自体今ブラケット創傷。細かい見出し見出し目的自体持っていました。\nピックバケツリンク自体。職人サワーカラム人形自体。癌ブランチ普通のデフォルト同行ヘア不自然な。\nあった偏差〜偏差今特徴敵。バストスパン拡張助けて。\nスマッシュ彼女ボトル隠す品質隠すサンプル。ヘアパイオニアスマッシュ風景。\nパン発生する装置尊敬する。偏差省略自体。'
Or we can also create text from a list of words
fake = Faker()my_information = ['dog','swimming', '21', 'slow', 'girl', 'coffee', 'flower','pink']fake.sentence(ext_word_list=my_information)
First run:
'Girl slow slow girl flower flower girl.'
and second run
'Flower 21 flower coffee flower dog.'
We can quickly create a profile with:
fake = Faker()fake.profile()
As we can see, most relevant information about a person is created with ease, even with mail, ssn, username, and website.
What is even more useful is that we can create a dataframe of 100 users from different countries
import pandas as pdfake = Faker(['it_IT','ja_JP', 'zh_CN', 'de_DE','en_US'])profiles = [fake.profile() for i in range(100)]pd.DataFrame(profiles).head()
Or create a customized profile
If we just care about the type of your data, without caring so much about the information, we can easily generate random datatypes such as:
Boolean
>>> fake.pybool()False
A list of 5 elements with different data_type
>>> fake.pylist(nb_elements=5, variable_nb_elements=True)['TiZaWQDCzVIgSALOSKJD',  8100,  'hZTFGZpYVwJUcGZUoauG',  Decimal('-3512.1951'),  37442800222.8852,  'XIxdbnwYMfOJDsZlaowZ']
A decimal with 5 left digits and 6 right digits (after the .)
>>> fake.pydecimal(left_digits=5, right_digits=6, positive=False, min_value=None, max_value=None)Decimal('92511.722977')
You can find more about other Python datatypes that you can create here
I hope you find Faker a helpful tool to create data efficiently. You may find this tool useful for what you are working on or may not at the moment. But it is helpful to know that there exists a tool that enables you to generate data with ease for your specific needs such as testing.
Feel free to fork and play with the code for this article in this Github repo or more information about Faker here.
I like to write about basic data science concepts and play with different algorithms and data science tools. You could connect with me on LinkedIn and Twitter.
Star this repo if you want to check out the codes for all of the articles I have written. Follow me on Medium to stay informed with my latest data science articles like these: