pythonbeginner

Generate Synthetic Data with Faker

Create realistic test datasets for development and testing using the Faker library.

python
from faker import Faker
import pandas as pd
import random

faker = Faker()
Faker.seed(42)
random.seed(42)

rows = [
    {
        'id':         i + 1,
        'name':       faker.name(),
        'email':      faker.email(),
        'address':    faker.address().replace('\n', ', '),
        'company':    faker.company(),
        'created_at': faker.date_time_between('-2y', 'now').isoformat(),
        'score':      round(random.uniform(0, 100), 2),
    }
    for i in range(1000)
]
df = pd.DataFrame(rows)
df.to_csv('synthetic_users.csv', index=False)
print(df.dtypes)

Use Cases

  • test data generation
  • demo datasets
  • ML training fixtures

Tags

Related Snippets

Similar patterns you can reuse in the same workflow.