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:
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
Now let's see step by step how to extract initials from the column Full_Name:
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
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
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
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 !
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!