Python function about chemical formulas

Question:

I have a CSV file that contains chemical matter names and some info.What I need to do is add new columns and write their formulas, molecular weights and count H,C,N,O,S atom numbers in each formula.I am stuck with the counting atom numbers part.I have the function related it but I don’t know how to merge it and make code work.

import pandas as pd    
import urllib.request    
import copy    
import re    

df = pd.read_csv('AminoAcids.csv')

def countAtoms(string, dict={}):
    curDict = copy.copy(dict)
    atoms = re.findall("[A-Z]{1}[a-z]*[0-9]*", string)

    for j in atoms:
        atomGroups = re.match('([A-Z]{1}[a-z]*)([0-9]*)', j)
        atom = atomGroups.group(1)
        number = atomGroups.group(2)
        try :
            curDict[atom] = curDict[atom] + int(number)
        except KeyError:
            try :
                curDict[atom] = int(number)
            except ValueError:
                curDict[atom] = 1
        except ValueError:
            curDict[atom] = curDict[atom] + 1
    return curDict

df["Formula"] = ['C3H7NO2', 'C6H14N4O2 ','C4H8N2O3','C4H7NO4 ',
'C3H7NO2S ','C5H9NO4','C5H10N2O3','C2H5NO2 ','C6H9N3O2',
'C6H13NO2','C6H13NO2','C6H14N2O2 ','C5H11NO2S ','C9H11NO2',
'C5H9NO2 ','C3H7NO3','C4H9NO3 ','C11H12N2O2 ','C9H11NO3 ','C5H11NO2']
df["Molecular Weight"] = ['89.09','174.2','132.12',
'133.1','121.16','147.13','146.14','75.07','155.15',
'131.17','131.17','146.19','149.21','165.19','115.13',
'105.09','119.12','204.22','181.19','117.15']
df["H"] = 0
df["C"] = 0
df["N"] = 0
df["O"] = 0
df["S"] = 0
df.to_csv("AminoAcids.csv", index=False)
print(df.to_string()) 
Asked By: sabina aliyeva

||

Answers:

If I understand correctly, you should be able to use str.extract here:

df["H"] = df["Formula"].str.extract(r'H(d+)')
df["C"] = df["Formula"].str.extract(r'C(d+)')
df["N"] = df["Formula"].str.extract(r'N(d+)')
df["O"] = df["Formula"].str.extract(r'O(d+)')
df["S"] = df["Formula"].str.extract(r'S(d+)')
Answered By: Tim Biegeleisen
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.