Python 如何将字典的键作为mergerd数据帧的索引?

Python 如何将字典的键作为mergerd数据帧的索引?,python,pandas,dataframe,Python,Pandas,Dataframe,我有一个名为percent\u dict的dict,里面有17个dfs 示例dfs: Hffpw key percentage 0 step19_without_lof 14.534883720930232 Hflpw key percentage 0 step19_without_lof 14.970930232558139 Bgf key

我有一个名为
percent\u dict
dict
,里面有17个
dfs

示例
dfs

Hffpw

          key                percentage
0   step19_without_lof  14.534883720930232
Hflpw

           key                percentage
0   step19_without_lof  14.970930232558139
Bgf

             key             percentage
0   step1_without_lof   1.5988372093023255
1   step2_without_lof   30.377906976744185
2   step5_without_lof   3.197674418604651
3   step7_without_lof   9.738372093023257
4   step12_without_lof  5.377906976744186
5   step15_without_lof  4.215116279069767
6   step16_without_lof  6.8313953488372094
7   step19_without_lof  13.80813953488372
8   step24_without_lof  9.883720930232558
9   step25_without_lof  11.337209302325581
10  step26_without_lof  9.738372093023257
11  step27_without_lof  9.738372093023257
等等

我已将其旋转如下:

def pivoting(df):
    d = pd.pivot_table(df, values = 'percentage', columns = ['key'])
    return d

pivoting('Hffpw')
pivoting('Hflpw')
pivoting('Bgf')
a = pd.concat(percent_dict.values())
旋转后的
dfs
命令如下所示:

def pivoting(df):
    d = pd.pivot_table(df, values = 'percentage', columns = ['key'])
    return d

pivoting('Hffpw')
pivoting('Hflpw')
pivoting('Bgf')
a = pd.concat(percent_dict.values())

我正在尝试合并所有这些数据帧(百分比中的值),键必须是结果数据帧的索引

我做了如下工作:

def pivoting(df):
    d = pd.pivot_table(df, values = 'percentage', columns = ['key'])
    return d

pivoting('Hffpw')
pivoting('Hflpw')
pivoting('Bgf')
a = pd.concat(percent_dict.values())
它给了我:

在图片中,我们可以看到
百分比
是索引。但是我想知道如何从
百分比中指定
键作为数据帧的索引。

一个想法是首先,然后将
多索引
转换为列,最后使用
透视表

df = (pd.concat(percent_dict)
        .reset_index()
        .pivot_table(index='level_0', values = 'percentage', columns = 'key'))

print (df)
key      step12_without_lof  step15_without_lof  step16_without_lof  \
level_0                                                               
Bgf                5.377907            4.215116            6.831395   
Hffpw                   NaN                 NaN                 NaN   
Hflpw                   NaN                 NaN                 NaN   

key      step19_without_lof  step1_without_lof  step24_without_lof  \
level_0                                                              
Bgf               13.808140           1.598837            9.883721   
Hffpw             14.534884                NaN                 NaN   
Hflpw             14.970930                NaN                 NaN   

key      step25_without_lof  step26_without_lof  step27_without_lof  \
level_0                                                               
Bgf               11.337209            9.738372            9.738372   
Hffpw                   NaN                 NaN                 NaN   
Hflpw                   NaN                 NaN                 NaN   

key      step2_without_lof  step5_without_lof  step7_without_lof  
level_0                                                           
Bgf              30.377907           3.197674           9.738372  
Hffpw                  NaN                NaN                NaN  
Hflpw                  NaN                NaN                NaN