Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (18.4k points)

I have this following dataset containing two columns full of 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 need however to convert those names in a 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 this first name followed by a dot.

1 Answer

0 votes
by (36.8k points)

The string methods are generally slow, but you can try the below which stacks a dataframe, a performs the 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.

If you are a beginner and want to know more about Python the do check out the python for data science course 

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...