my dictionary has tuples of (item, field) as keys and (1,1) dataframes as values with columns equal tuples (item, field), row index as a single date (same for all items), for example:
Key
('AB US Equity', 'CHG_PCT_1D')
('AB US Equity', 'PX_LAST')
('IBM US Equity', 'CHG_PCT_1D')
('IBM US Equity', 'PX_LAST')
Value
('AB US Equity', 'CHG_PCT_1D')
2/15/2019 0.5362
('AB US Equity', 'PX_LAST')
2/15/2019 30
here is the dic:
In [100]: str(dic)
Out[100]: "{('IBM US Equity', 'PX_LAST'): (IBM US Equity, PX_LAST)\n2019-02-15 138.03, ('IBM US Equity', 'CHG_PCT_1D'): (IBM US Equity, CHG_PCT_1D)\n2019-02-15 1.1357, ('AB US Equity', 'PX_LAST'): (AB US Equity, PX_LAST)\n2019-02-15 30.0, ('AB US Equity', 'CHG_PCT_1D'): (AB US Equity, CHG_PCT_1D)\n2019-02-15 0.5362}"
the below code puts them horizontally:
output=pandas.concat(dic.values(),axis=1)
output.columns=pandas.MultiIndex.from_tuples(output.columns)
output.columns.names=['Item','Field']
while having hundreds of items and a few fields I'd like to unpack it in the following vertical flat df:
Value CHG_PCT_1D PX_LAST
AB US Equity 0.5362 30
IBM US Equity 1.1357 138.03
SOLUTION (mainly based on the answer below)
import pandas as pd
dic = {('IBM US Equity', 'PX_LAST'): '2019-02-15 138.03',
('IBM US Equity', 'CHG_PCT_1D'):
'2019-02-15 1.1357',
('AB US Equity', 'PX_LAST'):
'2019-02-15 30.0',
('AB US Equity', 'CHG_PCT_1D'):
'2019-02-15 0.5362'}
output = pd.DataFrame.from_dict(dic, orient = 'index')
# to series removing date
output = output[0].str.split().str[-1]
# unstack help, first example
index = pd.MultiIndex.from_tuples(output.index)
output = pd.Series(output.values, index=index).unstack()