我有一个名为percent_dict
的dict
,里面有17dfs
样本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')
旋转后dfs
的dict如下所示:
我正在尝试合并所有这些数据帧(值在percent_dict
)并且键必须是结果数据帧的索引
我做了如下工作:
a = pd.concat(percent_dict.values())
它给了我:
在图中,我们可以看到percentage
是索引。但是我想知道如何从percent_dict
中指定Keys
作为数据帧的索引
一个想法是首先^{} ,然后将
MultiIndex
转换为列,最后使用pivot_table
:相关问题 更多 >
编程相关推荐