0

I have this dataframe:

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]})

Which looks like:

   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

I want to reshape it so it looks like this:

   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

My goal is to add additional cell lines, like from neutrophils or adipose, and plot the z-scores using boxplots, but I need the data in this form to do that. The order of the genes/cell lines doesn't matter as long as they match their respective z-scores. I am having trouble with pd.melt and pd.concat which leaves me unsure of their correct use.

Cam
  • 111
  • 1
  • 8
  • The cluster column is ambiguous . I am going to assume a cluster value a Gene applies to all siblings of cluster. – Golden Lion Feb 17 '21 at 17:01

2 Answers2

2

With 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
Psidom
  • 209,562
  • 33
  • 339
  • 356
0

I added a sort by Gene

results=pd.melt(df,id_vars=['Gene','Cluster'],var_name='Cell Lines',value_name='Z-Score')
print(results.sort_values(by='Gene',ascending=[False]))
Golden Lion
  • 3,840
  • 2
  • 26
  • 35