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

Published: January 28, 2022

Updated: December 09, 2022

Tags: Python; Pandas; DataFrame; Regex;

DMCA.com Protection Status

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 !