Replace strings in pandas dataframe / Memo

I would like to convert smiles string which has Cl or Br atoms convert to new one which has L as Cl and R as Br.
I wrote simple code for my note.

import pandas as pd
df = pd.read_table('cdk2.smi', sep=' ')
# make replace function with lumbda function
rep_func = lambda x: x.replace('Cl', 'L').replace('Br','R')
df['repsmi'] = df.SMILES.apply(rep_func)
#check
for row in df.repsmi:                                      
    ...:     if "L" in row:
    ...:         print(row)
    ...:     else: pass

Result was..

CC(C)C(CO)Nc1nc2c(ncn2C(C)C)c(Nc2cccc(L)c2)n1
CC(C)C(CO)Nc1nc2c(ncn2C(C)C)c(Nc2ccc(C(=O)[O-])c(L)c2)n1
C[NH+]1CCC(c2c(O)cc(O)c3c(=O)cc(-c4ccccc4L)oc32)C(O)C1

OK. If I use vectrize function the performance will be increased.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.