有没有办法在不使用元组的情况下创建在行和列上都具有多索引的数据框?我的标签太长而无法作为元组手动输入(每个国家96个国家和26个部门)。 Example of what I want
我试过了:
df_data.columns=label_df
df_data_w = pd.concat([label_df, data],axis=1,ignore_index=False)
这将标签df添加到前两列,但没有将其编入索引。我转而遵循dataframe
以下是一些要使用的代码:
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)`
这给我标签作为我的df中的标题,但我也需要它们作为列,所以我尝试了concat,它将标签df添加到前两列,但没有将其编入索引。
答案 0 :(得分:0)
您可以将zip
和list
与pd.MultiIndex
:
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']
indx = pd.MultiIndex.from_tuples(list(zip(Continent,Sectors)))
df.index = indx
df.columns = indx
print(df)