我有这个数据框架:
df = pd.DataFrame({'Gene': ['419', '1036', '1167', '1629'],
'Myob': [0.261, 1.010, -0.534, 1.412],
'Myob_Ind': [0.901, 0.525, 2.588, 1.825],
'Cluster': [0, 0, 0, 0]})看起来是这样的:
Gene Myob Myob_Ind Cluster
0 419 0.261 0.901 0
1 1036 1.010 0.525 0
2 1167 -0.534 2.588 0
3 1629 1.412 1.825 0我想重塑它,使它看起来像这样:
Gene Z-Scores Cell Lines Cluster
0 419 0.261 Myob 0
1 419 0.901 Myob_Ind 0
2 1036 1.010 Myob 0
3 1036 0.525 Myob_Ind 0
4 1167 -0.534 Myob 0
5 1167 2.588 Myob_Ind 0
6 1629 1.412 Myob 0
7 1629 1.825 Myob_Ind 0我的目标是添加额外的细胞系,比如来自中性粒细胞或脂肪细胞的细胞系,并使用箱形图绘制z得分,但我需要此表单中的数据来完成此操作。基因/细胞系的顺序并不重要,只要它们与各自的z分数相匹配即可。我在使用pd.melt和pd.concat时遇到了问题,这让我无法确定它们的正确使用。
发布于 2021-02-15 02:59:34
使用pd.melt
pd.melt(df, id_vars=['Gene', 'Cluster'], var_name='Cell Lines', value_name='Z-Score')
# Gene Cluster Cell Lines Z-Score
#0 419 0 Myob 0.261
#1 1036 0 Myob 1.010
#2 1167 0 Myob -0.534
#3 1629 0 Myob 1.412
#4 419 0 Myob_Ind 0.901
#5 1036 0 Myob_Ind 0.525
#6 1167 0 Myob_Ind 2.588
#7 1629 0 Myob_Ind 1.825发布于 2021-02-18 01:03:00
我添加了一个按基因排序
results=pd.melt(df,id_vars=['Gene','Cluster'],var_name='Cell Lines',value_name='Z-Score')
print(results.sort_values(by='Gene',ascending=[False]))https://stackoverflow.com/questions/66198924
复制相似问题