【问题标题】:exec() method invocations of Faker library in for loop | Pythonfor循环中Faker库的exec()方法调用| Python
【发布时间】: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


【解决方案1】:

eval() 完全符合我的要求。

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]:
        df[str(col_name)] = [eval(method) for _ in range(samples)]  # cols-1
    
    return df
df = example_dataset_simulation(500, 6)
df
>>> 500 rows × 6 columns

【讨论】:

    猜你喜欢
    • 2019-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-25
    • 2021-12-28
    • 1970-01-01
    • 2017-09-21
    相关资源
    最近更新 更多