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

Apply function 'str' object has no attribute 'str'

cause I don’t really understand why it works in one format and doesn’t work in another.

Works:

df['team'] = df['team'].str.extract(r'(\w+)+')

Doesn’t work:

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

def clear_teams(gr):
    return gr.str.extract(r'(\w+)+')
        
df['team'] = df['team'].apply(clear_teams)

I recive an error:

AttributeError: 'str' object has no attribute 'str'

Why it doesn’t work, can someone explain it to me ? pleas 🙂
How it has str attribute one time and another doesn’t ….

>Solution :

If use Series.apply then in function gr is scalar, function loop by element of Series. So cannot use Series functions for it like str.extract, but solution for processing by scalars:

def clear_teams(gr):
    try:
        return re.search(r'(\w+)+', text).group(1)
    except:
        return np.nan   

df['team'] = df['team'].apply(clear_teams)

If use Series.pipe then gr is Series, co all working correct:

def clear_teams(gr):
    return gr.str.extract(r'(\w+)+')

df['team'] = df['team'].pipe(clear_teams)

Or:

df['team'] = clear_teams(df['team'])
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