# Creating new columns based on 3 column and create new data frame

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

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

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

• Is this from a test / assignment ? Mar 24, 2019 at 9:12

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]


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

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