I've created a loop that produces a dataframe using input from a dictionary. I'd like to insert the ending dataframe into a new dictionary, name it the group number (12345), then start the loop over adding the new dataframe into the same dictionary, only it would have the new groups name (54321):
frames = {}
groups = ['12345', '54321']
for x in groups:
# Runs a bunch of calculations with the ending output being a pandas df
frames = df.to_dict()
Obviously I know this is wrong, I'm just scratching my head as to where I should go from here.
You can convert dictionary to pandas dataframe by creating a list of Dictionary items using the list(my_dict. items()) . Also, you can pass the column header values using the columns paramter.
You can create a DataFrame and append a new row to this DataFrame from dict, first create a Python Dictionary and use append() function, this method is required to pass ignore_index=True in order to append dict as a row to DataFrame, not using this will get you an error.
To create a Dictionary, use {} curly brackets to construct the dictionary and [] square brackets to index it. Separate the key and value with colons : and with commas , between each pair. As with lists we can print out the dictionary by printing the reference to it.
On Initialising a DataFrame object with this kind of dictionary, each item (Key / Value pair) in dictionary will be converted to one column i.e. key will become Column Name and list in the value field will be the column data i.e.
frames = {}
groups = ['123', '456']
for grp in groups:
#do some calcs to get a dataframe called 'df'
frames[grp] = df
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With