如何将以下类型的数据框转换为另一个数据框,其中列为目标列中的条目
Cq Target Sample Repeat
0 23.21562 NID1 Tgfb_48 4
1 23.31479 COL7A1 Tgfb_48 4
2 19.62652 COL1A2 Tgfb_48 4
3 20.99357 SERPINE1 Tgfb_48 4
4 25.26813 ELN Tgfb_48 4
即。
NID1 COL7A1 COL1A2 SERPINE ELN
[Sample, Repeat] ... cq values here
答案 0 :(得分:1)
df = df.set_index(['Sample','Repeat', 'Target'])['Cq'].unstack()
df.columns.name = None
print (df)
COL1A2 COL7A1 ELN NID1 SERPINE1
Sample Repeat
Tgfb_48 4 19.62652 23.31479 25.26813 23.21562 20.99357
另一种可能的解决方案,但这里是重复的聚合:
df = df.pivot_table(index=['Sample','Repeat'], columns='Target', values='Cq', aggfunc='np.mean)
print (df)
Target COL1A2 COL7A1 ELN NID1 SERPINE1
Sample Repeat
Tgfb_48 4 19.62652 23.31479 25.26813 23.21562 20.99357