I need to create a dynamic synthetic Pandas DF in Python and am using Faker and Random functions to create the same. To do so, I am creating a dictionary as a source to build pandas df where some columns will have static values whereas some column's values are generated through some expressions or Faker objects based on user input.\
for eg:
if user wants to create synthetic dataframe of size 5 with 2 columns, the user provides two inputs as arguments:
col1 = 1
col2 = tuple('1', '2')
so the dictionary should ideally be constructed as:
dict1 = {
'col1': 1,
'col2': fake.random_element(elements=('1','2'))
}
And the Dataframe can be constructed by running the expressions within the dictionary in a loop, as:
from faker import Faker
import pandas as pd
fake = Faker()
def generate_fake_data(numRows, inputDict):
output = [
inputDict for x in range(num)
]
return output
num_records = 5
df_sythetic = pd.DataFrame(generate_fake_data(num_records, dict1))
The problem is that if I try to create the dictionary without letting it evaluate the expression for col2 beforehand, it binds the value as string like:
dict1 = {
'col1': 1,
'col2': 'fake.random_element(elements=('1','2'))'
}
How can I create the dictionary without evaluating the expression columns and also not treating them as string ?
source https://stackoverflow.com/questions/76408258/python-create-a-dictionary-with-values-as-expressions-without-evaluating-or-tr
Comments
Post a Comment