using pd.append for rows

Viewed 39

I have a user defined function like this which takes in the argument as the row number and returns A string that joins two protein IDs with ':'

def join_ppi(row):
    
    if DF.loc[row].at["taxidA"] == '9606':
    
        var= DF.loc[row].at["prot1"] + ':' + DF.loc[row].at["prot2"] 
    
    else:
        var= DF.loc[row].at["prot2"] + ':' + DF.loc[row].at["prot1"]
           
    return(var)

I'm able to get the output for individual rows. But when I try to use pd.append for including this user defined function for all rows, I'm unable to do that. Can anyone help me on that?

I'm attaching the data, just for reference

https://i.stack.imgur.com/512ot.png

1 Answers

It looks like you want:

import numpy as np

df['new'] = np.where(df['taxidA'] == '9606',
                     df['prot1']+':'+df['prot2'],
                     df['prot2']+':'+df['prot1'])
Related