Reputation: 823
I want to extract initials from user names and next replace names with initials
s = pd.DataFrame(['Robert Flitz', 'Hans Malek', 'Marek J. Beer'])
when i try use this function
def initials(name):
init = ""
for n in name.str.split():
for i in n:
init+=i[0]
return init
it returns single string
initials(s[0])
'RFHMMJB'
I want to look it like
'RF'
'HM'
'MJB'
Any ideas?
Upvotes: 4
Views: 1548
Reputation: 19957
You can also use a regular expression:
s[0].str.replace('[^A-Z]', '')
0 RF
1 HM
2 MJB
Upvotes: 4
Reputation: 1500
A more pandas
approach would probably be:
def initials(name):
init = ""
for n in name.split():
init+=n[0]
return init
s[1] = s[0].apply(initials)
this gives:
0 1
0 Robert Flitz RF
1 Hans Malek HM
2 Marek J. Beer MJB
You can then get a list of initials with list(s[1])
if you need it in that format.
Upvotes: 0
Reputation: 43
You can edit your initials function. Append a new line to the init
variable after the end of the inner loop.
def initials(name):
init = ""
for n in name.str.split():
for i in n:
init+=i[0]
init+= "\n"
return init
Upvotes: 0
Reputation: 940
s[0].apply(lambda x: ''.join(i[0] for i in x.split()))
Output:
0 RF
1 HM
2 MJB
Name: 0, dtype: object
Change it numpy array
np.array(s[0].apply(lambda x: ''.join(i[0] for i in x.split())))
Output:
array(['RF', 'HM', 'MJB'], dtype=object)
Upvotes: 1