使用Python Faker为5000行生成不同的数据

时间:2017-08-08 17:05:37

标签: python-3.x pandas dataframe random faker

我想使用Python Faker库生成500行数据,但是我使用下面提到的代码获取重复数据。你能指出我出错的地方吗?我相信它与for循环有关。提前致谢:

from faker import Factory
import pandas as pd
import random

def create_fake_stuff(fake):


df = pd.DataFrame(columns=('name'
    , 'email'
    , 'bs'
    , 'address'
    , 'city'
    , 'state'
    , 'date_time'
    , 'paragraph'
    , 'Conrad'
    ,'randomdata'))

stuff = [fake.name()
    , fake.email()
    , fake.bs()
    , fake.address()
    , fake.city()
    , fake.state()
    , fake.date_time()
    , fake.paragraph()
    , fake.catch_phrase()
    , random.randint(1000,2000)]

for i in range(10):
        df.loc[i] = [item for item in stuff]
print(df)

if __name__ == '__main__':
    fake = Factory.create()
    create_fake_stuff(fake)

4 个答案:

答案 0 :(得分:1)

我将假东西数组放在我的for循环中以实现所需的结果:

paste

答案 1 :(得分:1)

以下脚本可以显着增强熊猫的性能。

    from faker import Faker
    import pandas as pd
    import random
    fake = Faker()
    def create_rows(num=1):
        output = [{"name":fake.name(),
                   "address":fake.address(),
                   "name":fake.name(),
                   "email":fake.email(),
                   "bs":fake.bs(),
                   "address":fake.address(),
                   "city":fake.city(),
                   "state":fake.state(),
                   "date_time":fake.date_time(),
                   "paragraph":fake.paragraph(),
                   "Conrad":fake.catch_phrase(),
                   "randomdata":random.randint(1000,2000)} for x in range(num)]
        return output

需要5.55秒。

    %%time
    df = pd.DataFrame(create_rows(5000))

    Wall time: 5.55 s

答案 2 :(得分:1)

免责声明:此答案在问题后添加了很多,并添加了一些新信息,而不直接回答问题。

现在有一个快速的新库Mimesis - Fake Data Generator

  • 上行:据说它的工作时间比faker快(请参阅下面的我的数据测试,类似于所讨论的数据)。
  • 缺点:仅适用于3.6版Python。

pip install mimesis

>>> from mimesis import Person
>>> from mimesis.enums import Gender
>>> person = Person('en')

>>> person.full_name(gender=Gender.FEMALE)
'Antonetta Garrison'
>>> personru = Person('ru')
>>> personru.full_name()
'Рената Черкасова'

与先前开发的faker相同:

pip install faker

>>> from faker import Faker
>>> fake_ru=Faker('ja_JP')
>>> fake_ru=Faker('ru_RU')
>>> fake_jp=Faker('ja_JP')
>>> print (fake_ru.name())
Субботина Елена Наумовна
>>> print (fake_jp.name())
大垣 花子

下面是我根据对forzer0eight的回答提供的代码,得出我最近对Mimesis vs. Faker的选择:

from faker import Faker
import pandas as pd
import random
fake = Faker()
def create_rows_faker(num=1):
    output = [{"name":fake.name(),
                   "address":fake.address(),
                   "name":fake.name(),
                   "email":fake.email(),
                   #"bs":fake.bs(),
                   "city":fake.city(),
                   "state":fake.state(),
                   "date_time":fake.date_time(),
                   #"paragraph":fake.paragraph(),
                   #"Conrad":fake.catch_phrase(),
                   "randomdata":random.randint(1000,2000)} for x in range(num)]
    return output

%%time
df_faker = pd.DataFrame(create_rows_faker(5000))

CPU时间:用户3.51 s,sys:2.86 ms,总计:3.51 s 挂墙时间:3.51 s

from mimesis import Person
from mimesis import Address
from mimesis.enums import Gender
from mimesis import Datetime
person = Person('en')
import pandas as pd
import random
person = Person()
addess = Address()
datetime = Datetime()
def create_rows_mimesis(num=1):
    output = [{"name":person.full_name(gender=Gender.FEMALE),
                   "address":addess.address(),
                   "name":person.name(),
                   "email":person.email(),
                   #"bs":person.bs(),
                   "city":addess.city(),
                   "state":addess.state(),
                   "date_time":datetime.datetime(),
                   #"paragraph":person.paragraph(),
                   #"Conrad":person.catch_phrase(),
                   "randomdata":random.randint(1000,2000)} for x in range(num)]
    return output

%%time
df_mimesis = pd.DataFrame(create_rows_mimesis(5000))

CPU时间:用户178毫秒,sys:1.7毫秒,总计:180毫秒 挂墙时间:179毫秒

下面是用于比较的结果数据:

df_faker.head(2)
address city    date_time   email   name    randomdata  state
0   3818 Goodwin Haven\nBrocktown, GA 06168 Valdezport  2004-10-18 20:35:52 joseph81@gomez-beltran.info Deborah Garcia  1218    Oklahoma
1   2568 Gonzales Field\nRichardhaven, NC 79149 West Rachel 1985-02-03 00:33:00 lbeck@wang.com  Barbara Pineda  1536    Tennessee

df_mimesis.head(2)
address city    date_time   email   name    randomdata  state
0   351 Nobles Viaduct  Cedar Falls 2013-08-22 08:20:25.288883  chemotherapeutics1964@gmail.com Ernest  1673    Georgia
1   517 Williams Hill   Malden  2008-01-26 18:12:01.654995  biochemical1972@yandex.com  Jonathan    1845    North Dakota

答案 3 :(得分:0)

使用farsantemimesis库是使用伪造数据创建Pandas DataFrame的最简单方法。

import random
import farsante
from mimesis import Person
from mimesis import Address
from mimesis import Datetime

person = Person()
address = Address()
datetime = Datetime()
def rand_int(min_int, max_int):
    def some_rand_int():
        return random.randint(min_int, max_int)
    return some_rand_int
df = farsante.pandas_df([
    person.full_name,
    address.address,
    person.name,
    person.email,
    address.city,
    address.state,
    datetime.datetime,
    rand_int(1000, 2000)], 5)

print(df)
        full_name              address    name  ...          state                   datetime some_rand_int
0   Weldon Durham   1027 Nellie Square   Bruna  ...  West Virginia 2030-06-10 09:21:29.179412          1453
1     Veta Conrad  932 Cragmont Arcade  Betsey  ...           Iowa 2017-08-11 23:50:27.479281          1909
2     Vena Kinney    355 Edgar Highway   Tyson  ...  New Hampshire 2002-12-21 05:26:45.723531          1735
3   Adam Sheppard    270 Williar Court  Treena  ...   North Dakota 2011-03-30 19:16:29.015598          1503
4  Penney Allison     592 Oakdale Road    Chas  ...          Maine 2009-12-14 16:31:37.714933          1175

这种方法可以使您的代码保持整洁。