The str()
and map()
function is really useful when working with strings in a pandas data frame.
Import the library:
import pandas as pd
Customer dataset:
Read the dataset:
df = pd.read_csv('Customers.csv')
print(df.head(20))
Output:
FirstName LastName Gender ... Profession Work Experience Family Size
0 Uriah Bridges Male ... Healthcare 1 4
1 Paula Small Female ... Engineer 3 3
2 Edward Buck Female ... Engineer 1 1
3 Michael Riordan Male ... Lawyer 0 2
4 Jasmine Onque Female ... Entertainment 2 6
5 Maruk Fraval Female ... Artist 0 2
6 Latia Costa Female ... Healthcare 1 3
7 Sharlene Terry Female ... Healthcare 1 3
8 Jac McKinzie Male ... Engineer 0 3
9 Joseph Martins Female ... Artist 1 4
10 Myriam Givens Male ... Engineer 1 3
11 Dheepa Nguyen Female ... Healthcare 4 4
12 Bartholemew Khemmich Female ... Executive 0 5
13 Xana Potts Female ... Lawyer 1 1
14 Prater Jeremy Male ... Doctor 0 1
15 Kaylah Moon Male ... Healthcare 1 2
16 Kristen Tate Female ... Homemaker 9 5
17 Bobby Rodgers Male ... Healthcare 1 6
18 Reid Park Male ... Entertainment 1 4
19 Hector Dalton Female ... Artist 0 1
[20 rows x 9 columns]
# New column Initials
df['Initials'] = df['FirstName'].str[0] + df['LastName'].str[0]
print(df[['FirstName', 'LastName', 'Initials']])
Output:
FirstName LastName Initials
0 Uriah Bridges UB
1 Paula Small PS
2 Edward Buck EB
3 Michael Riordan MR
4 Jasmine Onque JO
.. ... ... ...
195 Danika Fleming DF
196 Jocelyn Kline JK
197 Cesar Higgins CH
198 Gabriel Dodson GD
199 Raphael Sandoval RS
[200 rows x 3 columns]
map()
and lambda function):df['Initials'] = df['FirstName'].map(lambda s: s[0]) + df['LastName'].map(lambda s: s[0])
print(df['Initials'])