Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a dictionary of dataframes

Tags:

python

pandas

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.

like image 718
Milhouse Avatar asked Feb 29 '16 20:02

Milhouse


People also ask

Can you make a dictionary of Dataframes?

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.

How do I add a DataFrame to a dictionary?

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.

How do you create a dictionary?

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.

When we create DataFrame from dictionary of list then list becomes the?

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.


1 Answers

frames = {}

groups = ['123', '456']
for grp in groups:
    #do some calcs to get a dataframe called 'df'
    frames[grp] = df
like image 160
Sam Avatar answered Nov 14 '22 08:11

Sam