Bootstrap

使用python Fake 制造假数据/测试数据

第一次接触到Fake这个造数pyhton库,可以用来创造各种虚拟数据用来测试,记录一下一个简单的使用例子。


# pip install faker #安装
import pandas as pd 
import faker

f=faker.Faker("zh-cn") #输出为中文数据,输入“en-US”可输出英文

df=pd.DataFrame({
    "客户姓名":[f.name()for i in range(10)],
    "年龄":[f.random_int(25,40) for i in range(10)],
    "最后去点时间":[f.date_between(start_date='-1y',end_date='today')
             .strftime('%Y年%m月%d日')for i in range(10)],
    "地址":[f.street_address() for i in range(10)],
})

df

结果输出:
在这里插入图片描述

其他指令

from faker import Faker

# Create an instance of the Faker class
fake = Faker()

# Generate and print personal information
print("Name:", fake.name())
print("Address:", fake.address())
print("Email:", fake.email())
print("Phone Number:", fake.phone_number())
print("Social Security Number:", fake.ssn())
print("Date of Birth:", fake.date_of_birth())

# Generate and print internet-related information
print("Username:", fake.user_name())
print("Password:", fake.password())
print("Domain Name:", fake.domain_name())
print("IP Address:", fake.ipv4())
print("URL:", fake.url())

# Generate and print text
print("Words:", fake.words(nb=5))
print("Sentences:", fake.sentences(nb=3))
print("Paragraphs:", fake.paragraphs(nb=2))

# Generate and print company information
print("Company Name:", fake.company())
print("Catchphrase:", fake.catch_phrase())
print("Industry:", fake.industry())
print("Job Title:", fake.job())

# Generate and print financial information
print("Credit Card Number:", fake.credit_card_number())
print("Credit Card Expiry Date:", fake.credit_card_expire())
print("Credit Card Provider:", fake.credit_card_provider())

# Generate and print date and time
print("Current Date and Time:", fake.date_time())
print("Future Date:", fake.future_date(end_date="+30d"))
print("Past Date:", fake.past_date(start_date="-30d"))
print("Timezone:", fake.timezone())

# Generate and print geographic information
print("City:", fake.city())
print("Country:", fake.country())
print("Latitude and Longitude:", fake.latitude(), fake.longitude())
print("Postal Code:", fake.postcode())
print("Street Address:", fake.street_address())

# Generate and print miscellaneous information
print("Color:", fake.color_name())
print("ISBN Number:", fake.isbn13())
print("Vehicle Information:", fake.vehicle_make_model())
print("UUID:", fake.uuid4())

说明文档: https://faker.readthedocs.io/en/master/fakerclass.html#proxy-class-attribute-name-resolution

;