簡體   English   中英

for循環中Faker庫的exec()方法調用| Python

[英]exec() method invocations of Faker library in for loop | Python

使用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

其中 Output 是成功的。 每次生成一個唯一的數據集。

現在,我希望能夠更改 function 以添加n列,作為變量 integer 傳遞名為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

Output:

500 rows × 3 columns

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM