0

How can I convert the following type of dataframe into another dataframe with columns being the entries in the Target column

         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

i.e.

                   NID1    COL7A1    COL1A2    SERPINE    ELN
[Sample, Repeat]    ... cq values here

1 Answer 1

1

You can use set_index with unstack:

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

Another possible solution, but here is aggregation if duplicates:

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.