mercredi 29 septembre 2021

Python - Find a substring within a string using an IF statement when iterating through a pandas DataFrame with a FOR loop

I want to iterate through a column in a pandas DataFrame and manipulate the data to create a new column based on the existing column. For example...

For row in df['column_variable']:

      if 'substring1' in row:

              df['new_column'] = ...
      
      elif 'substring2' in row:

              df['new column'] = ...

      elif: 'substring3' in row:

              df['new column'] = ...

      else:

              df['new column'] = 'Not Applicable'

Even though type(row) returns 'str' meaning it is of the class string, this code keeps returning the new column as all 'Not Applicable' meaning it is not detecting any of the strings in any of the rows in the data frame even when I can see they are there.

I am sure there is an easy way to do this...PLEASE HELP!

I have tried the following aswell...

For row in df['column_variable']:

  if row.find('substring1') != -1:

          df['new_column'] = ...

  elif row.find('substring2') != -1:

          df['new column'] = ...

  elif: row.find('substring3') != -1:

          df['new column'] = ...

  else:

          df['new column'] = 'Not Applicable'

And I continue to get all entries of the new column being 'Not Applicable'. Once again it is not finding the string in the existing column.

Is it an issue with the data type or something?

Aucun commentaire:

Enregistrer un commentaire