I have the following dataset containing two columns full with names:
df = pd.DataFrame({'Player1':[ 'Roger Federer ', 'Alex De Minaur'], 'Player2':['Bernabe Zapata Miralles','Andy Roddick']})
with output:
Player1 Player2
0 Roger Federer Bernabe Zapata Miralles
1 Alex De Minaur Andy Roddick
I want however to convert these names in the pandas DataFrame so it looks like:
Player1 Player2
0 Federer R. Zapata Miralles B.
1 De Minaur A. Roddick A.
Full middle name(if present) - full surname - First letter of the first name followed by a dot.
There are a few comparable questions on stackoverflow that could help:
Preserve full surname, get initials of first name (and middle name if some) in pandas column Swapping first and last name positions
Anything would help!
string methods are generally slow, but you can try the below which stacks
the dataframe, the performs a str.split
, then unstack
back to original shape:
s = df.stack()
s1 = s.str.split()
out = s1.str[1:].str.join(" ").add(" "+s.str[0].str[0].add(".")).unstack()
print(out)
Player1 Player2
0 Federer R. Zapata Miralles B.
1 De Minaur A. Roddick A.