【发布时间】:2021-06-07 08:22:19
【问题描述】:
使用 Faker 库;我在 Jupyter Notebooks 中模拟一个数据集。
import numpy as np
import pandas as pd
from faker import Faker
fake = Faker()
import random
np.random.seed(42)
def example_dataset_simulation(samples):
df = pd.DataFrame(index=np.arange(samples))
df['ID'] = [str(i) for i in range(1, samples+1)]
df['Prefix'] = [fake.prefix_male() for _ in range(samples)]
df['Forename'] = [fake.first_name_male() for _ in range(samples)]
df['Surname'] = [fake.last_name_nonbinary() for _ in range(samples)]
return df
df = example_dataset_simulation(500)
df
输出成功。每次生成一个唯一的数据集。
现在,我希望能够更改函数以添加 n 列数,作为传递的变量整数,名为 cols。
所需的循环代码:
list = [["Prefix", "fake.prefix_male()"], ["Forename", "fake.first_name_male()"], ["Surname", "fake.last_name_nonbinary()"], ["Suffix", "fake.suffix_male()"], ["DOB", "fake.date()"], ["e-mail", "fake.company_email()"], ["Telephone", "fake.phone_number()"]]
def example_dataset_simulation(samples, cols):
df = pd.DataFrame(index=np.arange(samples))
df['Prefix'] = [fake.prefix_male() for _ in range(samples)] # once
for col_name, method in list[:cols-1]:
df[str(col_name)] = [eval(method) for _ in range(samples)] # cols-1
return df
输出:
500 rows × 3 columns
【问题讨论】:
-
你根本不应该在这里使用
exec -
你几乎肯定是指
"fake.prefix_male()"而不是fake.prefix_male()...但同样,不要在这里使用exec,无论如何,exec返回None -
嗯,好的。谢谢。将首先尝试
Faker方法作为string,看看exec()是否适用。如果没有,我将删除exec()。 -
@juanpa.arrivillaga 我已经在我的帖子中更新了下半部分的解决方案
-
exec在这里不起作用,你需要使用eval,但你不应该使用任何一个
标签: python string for-loop exec faker