I'm new to Python and could use some help. I have a function that takes a pandas DataFrame and returns an ordered dictionary to create a YAML file, and another function that takes the created ordered dictionary and performs a YAML dump. The function works for single dictionaries, but I want to add an additional function that can output multiple input dictionaries onto one YAML file.
Take these two DataFrames for example, and convert them into two ordered dictionaries:
df1:
Name Occupation Years JobSatisfaction
0 Lucy Black Teacher 10 Yes
1 John Doe Gardener 3 Yes
df2:
Name Age Salary Company Name
0 Lucy Black 31 $38000 LAUSD
1 John Doe 23 $17000 Beautiful Lawn
Now, how would you read both ordered dictionaries at once and get the YAML output to look like this:
-df1:
-Lucy Black:
-Occupation:Teacher
-Years:10
-JobSatisfaction:Yes
-John Doe:
-Occupation:Gardener
-Years:3
-JobSatisfaction:Yes
-df2:
-Lucy Black:
-Age: 31
-Salary: $38000
-Company Name: LAUSD
-John Doe:
-Age: 23
-Salary: $17000
-Company Name: Beautiful Lawn
Here is some skeleton code with my preferred code format:
def yaml_output(arguments)
function parameters
return()
What I have tried:
And this is what my code looks like so far:
def create_dict(df, key_index='uniqueID')
unordered_dict = df.set_index(key_index).T.to_dict()
ordered_dict = OrderedDict((k,unordered_dict.get(k)) for k in df[key_index])
return ordered_dict
def dump_ordered(dictionary)
yaml.add_representer(OrderedDict, lambda dumper,
data:dumper.represent_mapping('tag:yaml.org,2002:map',
data.items()))
return yaml.dump(dictionary)
Ultimately, I want to be able to pass the newly created function a variable number of dictionaries with each dictionary's name (could be a list of tuples?), so all dictionaries become one output YAML file. Let me know if you need additional clarification. Thank you!