Here is the dataframe
pid ,degree, board, university, certificateNumber, issuedate
1,'MMBS','American Medical Board', 'Medical Health University','M1234',2001-06-01
1,'MD',''American Medical Board', 'Medical Health University','G4564',1989-06-01
2,'NSC','Chiness Board', 'Bejing Univ', 'C12SD001', '2022-02-05'
3,'MBBS','Japan Univeresity',"Tokoyo University", '2015-05-15'
4,'MID','Maly Board', 'Jakartha University','JK1234','2022-09-15'
5,'MIDL','Germany Board', 'Berlin Unv', 'GB789', '2000-06-15'
python write a controled similatior function to create similar data frame as above , input would the columns with list id data to ingest .
Assistant
To create a controlled simulator function in Python that generates a dataframe similar to your example, we need to:
Accept input columns with their lists of data.
Generate pandas DataFrame.
For missing or incomplete rows, the function could simulate or fill data accordingly.
The input will be a dictionary with column names as keys and lists as values, simulating ingestion of data.
Here's a sample function using pandas:
python
import pandas as pd
from typing import Dict, List, Any
import random
from datetime import datetime, timedelta
def controlled_simulator(data: Dict[str, List[Any]]) -> pd.DataFrame:
"""
Simulate a controlled dataframe similar to your example.
Parameters:
-----------
data: Dict[str, List[Any]]
Dictionary where keys are column names and values are lists of column data.
Returns:
--------
pd.DataFrame
DataFrame containing the simulated data.
"""
# Convert to DataFrame directly
df = pd.DataFrame(data)
# Additionally, clean up and fix columns if needed
# For example, ensure issuedate is datetime type
if 'issuedate' in df.columns:
# Convert issuedate to datetime, handle errors coercing invalid dates to NaT
df['issuedate'] = pd.to_datetime(df['issuedate'], errors='coerce')
# Optionally, simulate or fill missing values based on the column
# For example, fill missing 'board' with a random choice
boards = ['American Medical Board', 'Chiness Board', 'Maly Board', 'Germany Board', 'Japan University Board']
if 'board' in df.columns:
df['board'] = df['board'].fillna(random.choice(boards))
# Similarly for 'degree', fill missing with random choice of medical degrees
degrees = ['MBBS', 'MD', 'MMBS', 'NSC', 'MID', 'MIDL']
if 'degree' in df.columns:
df['degree'] = df['degree'].fillna(random.choice(degrees))
# For certificateNumber, if missing, fill with generated random id string
if 'certificateNumber' in df.columns:
def gen_certnum():
return random.choice(['M','G','C','J','GB']) + str(random.randint(1000,9999))
df['certificateNumber'] = df['certificateNumber'].fillna(gen_certnum())
# If issuedate missing, fill with a random date between 1980-01-01 and 2024-01-01
if 'issuedate' in df.columns:
def random_date():
start_date = datetime(1980, 1, 1)
end_date = datetime(2024, 1, 1)
delta = end_date - start_date
random_day = random.randint(0, delta.days)
return start_date + timedelta(days=random_day)
df['issuedate'] = df['issuedate'].fillna(random_date())
return df
# Example usage:
data = {
'pid': [1,1,2,3,4,5],
'degree': ['MMBS','MD','NSC','MBBS','MID','MIDL'],
'board': ['American Medical Board','American Medical Board','Chiness Board','Japan University Board','Maly Board','Germany Board'],
'university': ['Medical Health University','Medical Health University','Bejing Univ','Tokoyo University','Jakartha University','Berlin Unv'],
'certificateNumber': ['M1234','G4564','C12SD001', None, 'JK1234', 'GB789'],
'issuedate': ['2001-06-01','1989-06-01','2022-02-05','2015-05-15','2022-09-15','2000-06-15']
}
df = controlled_simulator(data)
print(df)
Explanation:
The function accepts a dictionary of lists: keys represent columns, values their data.
The function builds a DataFrame and fixes incomplete or missing data as needed.
You can tweak the filling functions and lists to make the data more domain-specific.
issuedate is parsed to datetime; missing dates are replaced with random dates within a range.
Let me know if you want the simulator to generate fully random data or have specific rules!