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