Check substring in column value and append words in Pandas

Viewed 24

I am having difficulties in using the str functions when I iterate the df. It gave me "AttributeError: 'str' object has no attribute 'str'" . I checked the datatype and it is a str object.

I want to check it there is substring "fruit" exists in a column. If it does not exist, I would like to append the word "fruit" at the end of the existing value in the row. E.g. "strawberry" ->"strawberry fruit"

code:

for index, row in test_df1.iterrows():
  name = row['fruit']
  test =  row['fruit'].str.extract(r'(\w+)$')
  if test != 'fruit':
    row['fruit'] = test + " fruit"

I would appreciate some advice. Thank you.

1 Answers

Series.str contains "[v]ectorized string functions for Series and Index". So, indeed, row['fruit'] is a string and not a pd.Series. Hence, the error.

The easiest and fastest way to deal with your case, I think, is by simply using Series.replace:

import pandas as pd

df = pd.DataFrame({'fruit':['apple fruit','banana','fruit']})
df['fruit'].replace(r'(.*[^ fruit]$)',r'\1 fruit', regex=True, inplace=True)

print(df)

          fruit
0   apple fruit
1  banana fruit
2         fruit

(N.B. There is also Series.str.replace with some added functionality, but you don't need it here, and it is not inplace.)

Same result based on your method, but with str.rsplit:

for index, row in df.iterrows():
    name = row['fruit']
    test = name.rsplit(maxsplit=1)[-1]
    if test != 'fruit':
        row['fruit'] += ' fruit'

Or import re and change the line with test = ... into: test = re.search(r'(\w+)$',name).group(). So, this will work, but it is not vectorized, so it will be much, much slower on a large set.

Finally, np.where often comes in handy in such cases. Though here, again, it's not necessary. But just showing how you could have used it with Series.str.contains:

df['fruit'] = np.where(df['fruit'].str.contains(r'\bfruit$'),
                       df['fruit'],
                       df['fruit']+' fruit')
Related