Introducing pydbgen: A random dataframe/database table generator

Introducing pydbgen: A random dataframe/database table generator

Simple tool generates large database files with multiple tables to practice SQL commands for data science.

computer screen
Image by :


Get the newsletter

Join the 85,000 open source advocates who receive our giveaway alerts and article roundups.

When you start learning data science, often your biggest worry is not the algorithms or techniques but getting access to raw data. While there are many high-quality, real-life datasets available on the web for trying out cool machine learning techniques, I've found that the same is not true when it comes to learning SQL.

For data science, having a basic familiarity with SQL is almost as important as knowing how to write code in Python or R. But it's far easier to find toy datasets on Kaggle than it is to access a large enough database with real data (such as name, age, credit card, social security number, address, birthday, etc.) specifically designed or curated for machine learning tasks.

Wouldn't it be great to have a simple tool or library to generate a large database with multiple tables filled with data of your own choice?

Aside from beginners in data science, even seasoned software testers may find it useful to have a simple tool where, with a few lines of code, they can generate arbitrarily large data sets with random (fake), yet meaningful entries.

For this reason, I am glad to introduce a lightweight Python library called pydbgen. In this article, I'll briefly share some information about the package, and you can learn much more by reading the docs.

What is pydbgen?

Pydbgen is a lightweight, pure-Python library to generate random useful entries (e.g., name, address, credit card number, date, time, company name, job title, license plate number, etc.) and save them in a Pandas dataframe object, as an SQLite table in a database file, or in a Microsoft Excel file.

How to install pydbgen

The current version (1.0.5) is hosted on PyPI (the Python Package Index repository). You need to have Faker installed to make this work. To install Pydbgen, enter:

pip install pydbgen 

It has been tested on Python 3.6 and won't work on Python 2 installations.

How to use it

To start using Pydbgen, initiate a pydb object.

import pydbgen
from pydbgen import pydbgen

Then you can access the various internal functions exposed by the pydb object. For example, to print random US cities, enter:

>> 'Otterville'
for _ in range(10):
>> 8NVX937

By the way, if you enter city instead of city_real, it will return fictitious city names.

New Michelle
Lake Christopher
West Adamborough

Generate a Pandas dataframe with random entries

You can choose how many and what data types will be generated. Note that everything returns as string/texts.


The resulting dataframe looks like the following image.

Generate a database table

You can choose how many and what data types will be generated. Everything is returned in the text/VARCHAR data type for the database. You can specify the database filename and the table name.


This generates a .db file which can be used with MySQL or the SQLite database server. The following image shows a database table opened in DB Browser for SQLite.

Generate an Excel file

Similar to the examples above, the following code will generate an Excel file with random data. Note that phone_simple is set to False so it can generate complex, long-form phone numbers. This can come in handy when you want to experiment with more involved data extraction codes. 


The resulting file looks like this image:

Generate random email IDs for scrap use

A built-in method in pydbgen is realistic_email, which generates random email IDs from a seed name. This is helpful when you don't want to use your real email address on the web—but something close.

for _ in range(10):
    print(myDB.realistic_email('Tirtha Sarkar'))

Future improvements and user contributions

There may be many bugs in the current version—if you notice any and your program crashes during execution (except for a crash due to your incorrect entry), please let me know. Also, if you have a cool idea to contribute to the source code, the GitHub repo is open. Some questions readily come to mind:

  • Can we integrate some machine learning/statistical modeling with this random data generator?
  • Should a visualization function be added to the generator?

The possibilities are endless and exciting!

If you have any questions or ideas to share, please contact me at tirthajyoti[AT] If you are, like me, passionate about machine learning and data science, please add me on LinkedIn or follow me on Twitter. Also, check my GitHub repo for other fun code snippets in Python, R, or MATLAB and some machine learning resources. 

Originally published on Towards Data Science. Licensed under CC BY-SA 4.0.


About the author

Tirthajyoti Sarkar
Tirthajyoti Sarkar - Dr. Tirthajyoti Sarkar lives and works in the San Francisco Bay area as a senior technologist in the semiconductor domain, where he applies cutting-edge data science/machine learning techniques for design automation and predictive analytics. He contributes to open source software and writes regularly on various topics in data science, machine learning, and programming in top online platforms. He is also developing courseware on data wrangling and machine learning with leading publishing...