Thank you for the help! Based on your response/recommendation, I am thinking that my entire approach to solving my problem is very poor. I am trying to pull slices from a dataframe, store them in a nested dictionary, retrieve them, perform calculations, store the results in the same nested dictionary (hence why I need to know the actual name of each nested dictionary), retrieve the results, then format the results. LOL that looks so bad when I think about it out loud. What I think I should be doing is pull my first slice from a dataframe, perform my calculations, then format those results in my desired final output; take my second, third, fourth, etc. slices and then just repeat that same pattern instead of what I initially thought I should do. Is there a standard approach for evaluating particular slices of dataframes or does it always depend on the situation?
On Sun, Jun 7, 2020 at 4:39 AM Peter Otten <__pete...@web.de> wrote: > Aaron wrote: > > > When creating a nested dictionary of dataframes, how can I name a > > dictionary based on a list name of the dataframe? > > > > Given the following: > > > > # START CODE > > import pandas as pd > > > > cars = {'Brand': ['Honda Civic','Toyota Corolla'], > > 'Price': [22000,25000] > > } > > df_cars = pd.DataFrame(cars, columns = ['Brand','Price']) > > > > trucks = {'Brand': ['GMC Sierra','Ford F-150'], > > 'Price': [50000,48000] > > } > > df_trucks = pd.DataFrame(trucks, columns = ['Brand','Price']) > > > > list_of_dfs = [df_cars, df_trucks] > > > > # Not exactly sure how this code should be: > > dict_of_dfs = {} > > for df in list_of_dfs: > > dict_of_dfs[name_of_df] = {} # Not sure here > > dict_of_dfs[name_of_df]['results'] = df # Not sure here > > # END CODE > > In the general case you can't find the name of a value, so you may start > with the names and look up the dataframes in the global namespace: > > list_of_df_names = ["df_cars", "df_trucks"] > dict_of_dfs = { > name: {"results": globals()[name]} > for name in list_of_df_names > } > > Personally I would probably reorganize your code a bit and avoid the > df_... > names: > > import pandas as pd > > vehicles = dict( > cars={ > 'Brand': ['Honda Civic','Toyota Corolla'], > 'Price': [22000,25000] > }, > trucks={ > 'Brand': ['GMC Sierra','Ford F-150'], > 'Price': [50000,48000] > } > ) > > dict_of_dfs = { > name: {"results": pd.DataFrame(val)} > for name, val in vehicles.items() > } > > > -- > https://mail.python.org/mailman/listinfo/python-list > -- https://mail.python.org/mailman/listinfo/python-list