Is there a way to create a dataframe having multi-indexing on both rows and columns without using tuples? My labels are too long to enter manually as tuples (96 countries and 26 sectors per country). Example of what I want
I tried:
df_data.columns=label_df
df_data_w = pd.concat([label_df, data],axis=1,ignore_index=False)
This added the label df to the first two columns, but didn't index it. I instead get this following dataframe
Here is some code to use:
import numpy as np
import pandas as pd
a = np.random.randint(low=0, high=10,size=9)
b = np.random.randint(low=0, high=10,size=9)
c = np.random.randint(low=0, high=10,size=9)
d = np.random.randint(low=0, high=10,size=9)
e = np.random.randint(low=0, high=10,size=9)
f = np.random.randint(low=0, high=10,size=9)
g = np.random.randint(low=0, high=10,size=9)
h = np.random.randint(low=0, high=10,size=9)
i = np.random.randint(low=0, high=10,size=9)
df = pd.DataFrame(data=[a,b,c,d,e,f,g,h,i])
Continent = ['Africa','Africa','Africa','North America', 'North America', 'North America', 'Europe','Europe','Europe']
Sectors = ['Agriculture','Industry','Domestic','Agriculture','Industry','Domestic','Agriculture','Industry','Domestic']
label_df = pd.DataFrame(data=[Continent, Sectors])
df.columns=label_df
df_w_labels = pd.concat([label_df, data],axis=1,ignore_index=False)`
This gives me the labels as headers in my df, but I need them as columns as well, so I tried concat, which added the label df to the first two columns, but didn't index it.