Reputation: 103
I had a task of extracting the email name from fullname as below ( and my code to deal with it)
list_=['peter mary','peter mary david ','pop david','peter pop ronaldo bulma']
sr=pd.Series(list_,range(4))
sr_split=sr.str.split()
first_name=sr_split.str[-1]
other_name=sr_split.str[0:-1]
other_name=other_name.str.join(' ')
other_=other_name.str.split(expand=True)
other_.fillna(' ',inplace=True)
add_name=other_[0].str[0]+other_[1].str[0]+other_[2].str[0]
email_name=pd.concat([first_name,add_name],axis=1)
email_name[2]=email_name[0]+email_name[1]
My code returned:
maryp, davidpm,davidp,bulmappr
It worked as my expectation.However,there are 2 problems with my code: 1) Use a lot of split and join 2) Can not work with long name, say : name with 10 words
Is it a way to make it better?
Upvotes: 1
Views: 71
Reputation: 76947
How about using apply and string methods?
In [469]: (sr.str.strip().str.split(' ')
.apply(lambda x: x[-1] + ''.join([y[0] for y in x[:-1]]))
)
Out[469]:
0 maryp
1 davidpm
2 davidp
3 bulmappr
dtype: object
Upvotes: 1
Reputation: 2293
This should do it.
list_=['peter mary','peter mary david ','pop david','peter pop ronaldo bulma']
def mk_email_name(x):
#get names
names = x.split(' ')
if len(names) == 1:
return x
else:
#get first name
fn = names[0]
#get last_names
lns = ''.join(map(lambda y:y[:1], names[1:]))
email = '%s%s' % (fn, lns)
return email
#apply without pandas
print map(mk_email_name, list_)
# ['peterm', 'petermd', 'popd', 'peterprb']
#apply on pandas df
df = pd.DataFrame(list_, columns=['full_name'])
df['email'] = df.full_name.apply(mk_email_name)
Upvotes: 0