You could loop through your dictionary and create a new dictionary of dictionaries, where the outer dictionary's keys are column names and the inner dictionaries' keys are the row indices. To save on a few lines of code, I'm going to use a defaultdict(dict)
as the outer dictionary
from collections import defaultdict
import pandas as pd
dictionary = {('a','c'): 1, ('a','d'): 3,
('b','c'): 2, ('b','d'): 4}
dd = defaultdict(dict)
for (col_name, row_name), value in dictionary.items():
dd[col_name][row_name] = value
This results in the following dd
:
defaultdict(<class 'dict'>, {'a': {'c': 1, 'd': 3}, 'b': {'c': 2, 'd': 4}})
Finally, use this to create your dataframe:
df = pd.DataFrame.from_dict(dd)
Which gives the desired dataframe:
a b
c 1 2
d 3 4