我有一个数据框,其中包含一些由pandas concat
函数生成的外键,看起来像这样
ID ratio log_q
L-D 0 A5A614 2.51803 2.09644
1 P00370 3.76811 5.92205
2 P00393 1.74254 3.74875
3 P00452-2 3.37144 6.13225
4 P00547 3.06521 5.55512
5 P00561 3.02943 5.58718
ID ratio log_q
M-D 0 A5A614 2.51803 2.09644
1 P00370 3.76811 5.92205
2 P00393 1.74254 3.74875
3 P00452-2 3.37144 6.13225
4 P00547 3.06521 5.55512
5 P00561 3.02943 5.58718
ID ratio log_q
M3-D 0 A5A614 2.51803 2.09644
1 P00370 3.76811 5.92205
2 P00393 1.74254 3.74875
3 P00452-2 3.37144 6.13225
4 P00547 3.06521 5.55512
5 P00561 3.02943 5.58718
我想再次使用concat
生成一个新的数据框,其中所有键的比率列(' L-D', ' M-D',' M3-D')并使用这些键作为新列的名称。
此外,应按以下方式对齐新数据框,以便匹配' ID' :
L-D M-D M3-D
A5A614 2.51803 1.13223 2.64402
P00393 3.76811 1.97461 3.34965
P00547 1.74254 2.70024 2.3655
...
当我使用
时pd.concat([df.ix['L-D']['ratio'], df.ix['M-D']['ratio'], df.ix['M3-D']['ratio']],
axis=1, levels=("L-D","M-D","M3-D"))
或
pd.concat([df.ix['L-D']['ratio'], df.ix['M-D']['ratio'], df.ix['M3-D']['ratio']],
axis=1, names=("L-D","M-D","M3-D"))
我可以创建一个数据框但结果如下:
ratio ratio ratio
0 2.51803 1.13223 2.64402
1 3.76811 1.97461 3.34965
2 1.74254 2.70024 2.3655
显然,没有使用名称/级别,它只是采用数字索引,而不是' ID'
答案 0 :(得分:1)
我认为您需要将参数keys
添加到concat
而不是levels
:
#remove first level and append column ID:
df = df.reset_index(level=1, drop=True).set_index('ID', append=True)
print pd.concat([df.ix['L-D']['ratio'], df.ix['M-D']['ratio'], df.ix['M3-D']['ratio']],
axis=1,
keys=["L-D","M-D","M3-D"])
L-D M-D M3-D
ID
A5A614 2.51803 2.51803 2.51803
P00370 3.76811 3.76811 3.76811
P00393 1.74254 1.74254 1.74254
P00452-2 3.37144 3.37144 3.37144
P00547 3.06521 3.06521 3.06521
P00561 3.02943 3.02943 3.02943
但我认为更好的是使用pd.pivot
与get_level_values
:
print pd.pivot(index=df.ID, columns=df.index.get_level_values(0), values=df.ratio)
L-D M-D M3-D
ID
A5A614 2.51803 2.51803 2.51803
P00370 3.76811 3.76811 3.76811
P00393 1.74254 1.74254 1.74254
P00452-2 3.37144 3.37144 3.37144
P00547 3.06521 3.06521 3.06521
P00561 3.02943 3.02943 3.02943