How to create a new column of initials from a full name dataframe column with pandas and python ?

Active January 28, 2022    /    Viewed 402    /    Comments 0    /    Edit


Example of how to create initials from a full name dataframe column with pandas and python:

Create a dataframe with pandas

Let's first create a data with pandas with a column with full names:

import pandas as pd
import numpy as np

data = {'Full_Name':['April Reiter','Emory Miller','David Ballin','Alice Trotter','Virginia Rios']}

df = pd.DataFrame(data=data)

print(df)

gives

             Full_Name
0   April Reiter
1   Emory Miller
2   David Ballin
3  Alice Trotter
4  Virginia Rios

Create a new column with initials

Now let's see step by step how to extract initials from the column Full_Name:

Step1: split first and last names using pandas.Series.str.split:

df['Full_Name'].str.split(expand=True)

gives

                    0        1
0     April   Reiter
1     Emory   Miller
2     David   Ballin
3     Alice  Trotter
4  Virginia     Rios

Step 2: exatract first letter for each columns:

df['Full_Name'].str.split(expand=True).apply(lambda x : x.str[0])

gives

     0  1
0  A  R
1  E  M
2  D  B
3  A  T
4  V  R

Step 3: aggregate column 0 and 1:

df['Full_Name'].str.split(expand=True).apply(lambda x : x.str[0]).agg('.'.join, axis=1)

gives

0    A.R
1    E.M
2    D.B
3    A.T
4    V.R
dtype: object

Step 4: Let's create a new column to store the initials:

df['Initials'] = df['Full_Name'].str.split(expand=True).apply(lambda x : x.str[0]).agg('.'.join, axis=1)

gives

             Full_Name Initials
0   April Reiter      A.R
1   Emory Miller      E.M
2   David Ballin      D.B
3  Alice Trotter      A.T
4  Virginia Rios      V.R

done !

References


Card image cap
profile-image
Daidalos

Hi, I am Ben.

I have developed this web site from scratch with Django to share with everyone my notes. If you have any ideas or suggestions to improve the site, let me know ! (you can contact me using the form in the welcome page). Thanks!



Did you find this content useful ?, If so, please consider donating a tip to the author(s). MoonBooks.org is visited by millions of people each year and it will help us to maintain our servers and create new contents.

Amount