Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Loop through rows of a column and get first word

I am trying to loop through a column and check to see if the first word equals something. If it does, then delete everything except for the first two words. Otherwise, only keep the first word. Below is what I tried, but I am just getting a syntax error at the first IF statement. Thanks in advance.

for name in df.NDCName:
        if df['NDCName'].split()[0] = 'Invega':
            df['NDCName'].apply(lambda x: x.split()[:2])
        else:
            df['NDCName'].apply(lambda x: x.split()[0])

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

When comparing, you need to use ==. Using the = tries to set df['NDCName'].split()[0] to "Invega".

So:

if df['NDCName'].split()[0] == 'Invega':
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading