I have written code to split full name to First, Middle and Last:
import pandas as pd
df= pd.read_excel(r"D:\SAMPLE EXCEL FILES\names.xlsx")
splitted = df['Names'].str.split()
first = splitted.str[0]
middle = splitted[1]
last = splitted.str[-1]
df['First Name'] = first
df['Middle Name'] = middle.mask(middle == last,'')
df['Last Name'] = last
print(df)
I am getting following error:
Traceback (most recent call last):
File "D:/names.py", line 18, in <module>
df['Middle Name'] = middle.mask(middle == last,'')
AttributeError: 'list' object has no attribute 'mask'
Here is the sample data:
Names
James Michael Anderson
William Smith
Sophia James
Mia Charlotte Moore
Christopher Harris
Ella Catherine Walker
Daniel Wilson
The print sequence is important, First, Middle and Last.
I am unable to resolve this issue.
The issue is resolved after correcting at typo error in the code. Here is the updated code:
import pandas as pd
df= pd.read_excel(r"D:\SAMPLE EXCEL FILES\names.xlsx")
splitted = df['Names'].str.split()
first = splitted.str[0]
middle = splitted.str[1]
last = splitted.str[-1]
df['First Name'] = first
df['Middle Name'] = middle.mask(middle == last,'')
df['Last Name'] = last
print(df)