Reputation: 4149
I have one dictionary of several pandas dataframes. It looks like this:
key Value
A pandas dataframe here
B pandas dataframe here
C pandas dataframe here
I need to extract dataframes from dict as a separate part and assign dict key as a name.
Desired output should be as many separate dataframes as many values of my dict have.
A = dict.values() - this is first dataframe
B = dict.values() - this is second dataframe
Note that dataframes names are dict keys.
I tried this code but without any success.
for key, value in my_dict_name.items():
key = pd.DataFrame.from_dict(value)
Any help would be appreciated.
Upvotes: 3
Views: 2785
Reputation: 862581
It is not recommended, but possible:
Thanks @ Willem Van Onsem for better explanation:
It is a quite severe anti-pattern, especially since it can override existing variables, and one can never exclude that scenario
a = pd.DataFrame({'a':['a']})
b = pd.DataFrame({'b':['b']})
c = pd.DataFrame({'c':['c']})
d = {'A':a, 'B':b, 'C':c}
print (d)
{'A': a
0 a, 'B': b
0 b, 'C': c
0 c}
for k, v in d.items():
globals()[k] = v
print (A)
a
0 a
I think here the best is MultiIndex
if same columns or index values in each DataFrame
, also dictionary of DataFrame is perfectly OK.
Upvotes: 3