A dictionary of tuples can create a structured DataFrame with hierarchically indexed rows and columns. The following is a dictionary of tuples:
salesData = {("2012", "Q1"): {("North", "Brand A"): 100, ("North", "Brand B"): 80, ("South", "Brand A"): 25, ("South", "Brand B"): 40},
("2012", "Q2"): {("North", "Brand A"): 30, ("South", "Brand B"): 50},
("2013", "Q1"): {("North", "Brand A"): 80, ("North", "Brand B"): 10, ("South", "Brand B"): 25},
("2013", "Q2"): {("North", "Brand A"): 70, ("North", "Brand B"): 50, ("South", "Brand A"): 35, ("South", "Brand B"): 40}}
Instead of a regular key-value pair, the key is a tuple with two values denoting two levels in the row index, and the value is a dictionary in which each key-value pair represents a column. Here, again, the key is a tuple and denotes two column indices.
Now this dictionary of tuples can be converted to a DataFrame and printed:
salesDF = pd.DataFrame(salesData)
The following is the output: