Search code examples
pythonfakersynthetic

Filling a data frame with synthetic data


I am trying to fill and save a DataFrame with fake data from the Faker package but I am struggling to return more that one row of data.

I tried using a for loop but it didn't quite work. Could anybody point me in the right direction?

from faker import Faker
import pandas as pd

fake = Faker()

def create_df(range):
    
    data={'Name':[fake.name()],'address':[fake.address()],"email":[fake.email()] }
    df=pd.DataFrame(data)

    return df

df

Thanks.


Solution

  • You can try this modified code:

    from faker import Faker
    import pandas as pd
    
    fake = Faker()
    
    def create_df(rows_count):
    
        data=[{'Name':fake.name(),'address':fake.address(),"email":fake.email() } for x in range(rows_count)]
        df=pd.DataFrame(data)
    
        return df
    

    rows_count, as the name suggest serves to select the number of rows you want your dataset to have.

    Hope it helps.