Question: How to pivot pandas dataframe?
0
gravatar for kristopherfernandez
14 months ago by
kristopherfernandez10 wrote:

Given a dataframe:

 Sample_Name SNP1 SNP2 SNP3
 Sample 1 AA AC GC
 Sample 2 GA AC CG
 Sample 3 AA AA GG

How do I get:

 Sample_Name SNP Value
 Sample 1 SNP1 AA
 Sample 1 SNP2 AC
 Sample 1 SNP3 GC
 Sample 2 SNP1 GA
 Sample 2 SNP2 AC
 Sample 2 SNP3 CG
 ...

I'm a beginner and I've been trying to find a way to do this but none of the options I've tried have been successful. Thanks to anyone who could help!

pivot dataframe pandas python • 401 views
ADD COMMENTlink written 14 months ago by kristopherfernandez10
2

Minor point: You're unpivoting the data (going from wide-form to long-form), not pivoting it.

ADD REPLYlink written 14 months ago by RamRS27k
1
gravatar for finswimmer
14 months ago by
finswimmer13k
Germany
finswimmer13k wrote:

Like this?

import pandas as pd

df = pd.DataFrame(
    [["AA", "AC", "GC"], ["GA", "AC", "CG"], ["AA", "AA", "GG"]],
    index=["Sample 1", "Sample 2", "Sample 3"],
    columns=["SNP1", "SNP2", "SNP3"],
)

print(df.stack())

Output:

Sample 1  SNP1    AA
          SNP2    AC
          SNP3    GC
Sample 2  SNP1    GA
          SNP2    AC
          SNP3    CG
Sample 3  SNP1    AA
          SNP2    AA
          SNP3    GG
dtype: object
ADD COMMENTlink written 14 months ago by finswimmer13k

Once you have this object, how can you subset / filter to find, say, all samples with SNP1 'AA'?

ADD REPLYlink written 14 months ago by kristopherfernandez10
1

For this task there is no need to convert it. You can use:

df.index[df['SNP1'].str.match('AA')].tolist()
ADD REPLYlink written 14 months ago by finswimmer13k
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1919 users visited in the last hour