mercredi 3 mai 2017

Swapping rows between columns within dataframe for multiple dataframe based on condition

I have a dataframe which looks like following,

foo = pd.DataFrame(
        [['chr1',2,1,'+',0.1,'NA','TSS1'],
        ['chr2',3,4,'-',0.03,'NA','TSS2'],
        ['chr3',7,6,'+',0.7,'NA','TSS3']], 
        columns = ('CHR', 'start', 'end','Strand','Peak','Ratio','Annotation')
    )
foo
    CHR start   end Strand  Peak    Ratio   Annotation
0   chr1    2   1   +   0.10    NA  TSS1
1   chr2    3   4   -   0.03    NA  TSS2
2   chr3    7   6   +   0.70    NA  TSS3

And I am aiming to swap between the column start and End, ie if the column start is greater than column end then I need it to swap its position and keep the rest of columns intact or just as it is.

something like this,

def fun(x):
   if df['start']> df['End']
print df[['CHR','end','start','Strand','Peak','Ratio','Annotation']]
   else
  return df

The above function doesn't work as I needed. At the end, I need a dataframe,

    CHR   start  end    Strand  Peak    Ratio   Annotation
0   chr1    1   2   +   0.10    NA  TSS1
1   chr2    3   4   -   0.03    NA  TSS2
2   chr3    6   7   +   0.70    NA  TSS3

Any help or better suggestions would be great. Also, I have large multiple data frames.

Aucun commentaire:

Enregistrer un commentaire