How to get the first letter of each word in a string of dataframe column


This Content is from Stack Overflow. Question asked by Matas M

I have a dataframe column of first and last names. I want to extract the initials from the names as another column in my dataframe. For the following dataframe:

0 'Brad Pitt'
1 'Bill Gates'
2 'Elon Musk'

I have came up with a solution:

df['initials'] = [df['Name'][i].split()[0][0] + df['Name'][i].split()[1][0] for i in range(len(df))]

However, for a name such as ‘John David Smith’, this does not work, as I want to have the first letter of each word in a name. Moreover, since my dataframe is quite large, I would like to know if there is a ‘vectorized’ solution (without for loops).

Thank you in advance.


This question is not yet answered, be the first one who answer using the comment. Later the confirmed answer will be published as the solution.

This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?