Home > Mobile >  Loop through rows of a column and get first word
Loop through rows of a column and get first word

Time:01-06

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])

CodePudding user response:

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

So:

if df['NDCName'].split()[0] == 'Invega':
  •  Tags:  
  • Related