I have a dataframe with a column of phone numbers which I'd like to map to country (as a new column "phone_country"), based on their country code. I realise there's probably a Python library to do this, but right now I'd like to understand why I get the error message "'str' object has no attribute 'str'", 'occurred at index 0'"
I did try to ensure that the phone_number column was a string df['phone_number'] = df['phone_number'].apply(str)
def phone_country(df):
if df['phone_number'].str[:1] == '7':
value = 'RUS'
elif (df['phone_number'].str[:2] == '34'):
value = 'ESP'
elif (df['phone_number'].str[:2] == '39'):
value = 'ITA'
elif (df['phone_number'].str[:3] == '212'):
value = 'MAR'
else:
value = 'Other'
return value
df['phone_country'] = df.apply(phone_country, axis=1)
Thanks in advance, I'm pretty new to Python.
Aucun commentaire:
Enregistrer un commentaire