Reputation: 1001
I'm busy with the Titanic kaggle and I want to simplify and Pythonify this piece of code:
allData = [trainData, testData]
commonTitles = {'Mlle' : 'Miss', 'Ms' : 'Miss', 'Mme' : 'Mrs', all others : 'Rare'}
for dataset in allData:
dataset['Title'].map(commonTitles)
How do I specify the all others part? Also, will the map command skip 'Miss' and 'Mrs' here or will it change them to 'Rare'?
Thank you
Upvotes: 0
Views: 1358
Reputation: 863226
First change dictionary by remove Rare
and add 2 elements with same key and value for avoid replace them to Rare
, add Series.fillna
for replace no match values and because loop list of DataFrame create another one:
commonTitles = {'Mlle' : 'Miss',
'Ms' : 'Miss',
'Miss':'Miss',
'Mme' : 'Mrs',
'Mrs': 'Mrs'}
dfs = []
for dataset in allData:
dataset['Title' ] = dataset['Title' ].map(commonTitles).fillna('Rare')
dfs.append(dataset)
Upvotes: 1