1
$\begingroup$

Heading

Consider my data frame rs123 T C 0 0 1 1 0 0 1 0 0 1 0 0 rs124 T C 0 0 1 0 0 1 0 0 1 0 0 1 rs125 A A 1 0 0 1 0 0 1 0 0 1 0 0

Similarity, i have total 93 columns excluding first three

I want to create my data as

enter image description here

And then transform into new data frame as below

  1. For first row if 1 is present in column 1 then output should be TT
  2. For first row if 1 is present in column 2 then output should be TC
  3. For first row if 1 is present in column 3 then output should be CC

For more detail you can refer below snip

enter image description here

Kindly help me to find solution using python, Its very urgent

Thanks in Advance.

$\endgroup$
1
  • $\begingroup$ Is this from a test / assignment ? $\endgroup$ Commented Mar 24, 2019 at 9:12

1 Answer 1

0
$\begingroup$

The question could have been framed better. Checkout the code below, in which your final dataframe will be in output.

import pandas as pd

input_array = [["rs123", "T", "C", 0, 0, 1, 1, 0, 0, 1, 0, 0], ["rs124", "A", "G", 0, 0, 1, 0, 1, 0, 0, 0, 1]]

raw_pd = pd.DataFrame(input_array).astype(str)

def change(a):
    if list(a)[2]+list(a)[3]+list(a)[4] == "100":
        return list(a)[0] + list(a)[0]
    elif list(a)[2]+list(a)[3]+list(a)[4] == "010":
        return list(a)[0] + list(a)[1]
    else:
        return list(a)[1] + list(a)[1]

output = pd.DataFrame()

output['S1'] = raw_pd[[1, 2, 3, 4, 5]].apply(lambda x: change(x), axis = 1)

output['S2'] = raw_pd[[1, 2, 6, 7, 8]].apply(lambda x: change(x), axis = 1)

output['S3'] = raw_pd[[1, 2, 9, 10, 11]].apply(lambda x: change(x), axis = 1)

output['SNP'] = raw_pd[0]

enter image description here

Hope this helps ;) Mark this as the correct answer if you have no other doubts.

$\endgroup$
1
  • $\begingroup$ Hi William, Thanks for your help. This worked (Y) Also, regarding question framing will take care of it. $\endgroup$ Commented Mar 24, 2019 at 12:00

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.