When working with dataset "titanic" I encountered the following problem: it is necessary to select a name depending on the title of the passenger, the allocation rules are different, I wrote a function:
def name_extract(x): title=x['Title'] name=x['Name'] if title in ['Mr','Miss']: return name.split('.')[1].split(' ')[1].strip() elif title in ['Mrs']: return name.split('(')[1].split(' ')[1].strip() The first part of the function for Mr and Miss works without problems, but the second gives an error
IndexError: ('list index out of range', 'occurred at index 20') Dataset contains the Name field - this is the full name of the passenger, from which the First name must be distinguished. The rules are different depending on the Title , for example, for Mr with a space, and for Mrs with a bracket:
Braund, Mr. Owen Harris, Futrelle, Mrs. Jacques Heath (Lily May Peel)
Title is also allocated by the rules and grouped. This stage is solved without problems.
def name_extract(word): return word.split(',')[1].split('.')[0].strip() def replace_titles(x): title=x['Title'] if title in ['Don', 'Major', 'Capt', 'Jonkheer', 'Rev', 'Col', 'Master','Sir']: return 'Mr' elif title in ['Countess', 'Mme','Lady']: return 'Mrs' elif title in ['Mlle', 'Ms']: return 'Miss' elif title =='Dr': if x['Sex']=='Male': return 'Mr' else: return 'Mrs' else: return title
'Mr','Miss'lists? Solution requires more data. Desirable for more code. The error says that somewhere you requested a list item by index, which is not in the list. - New Python Programmist'Futrelle, Mrs. Jacques Heath (Lily May Peel)''Futrelle, Mrs. Jacques Heath (Lily May Peel)'? - MaxU