I have a dataframe of teachers and chaperones on a school trip.
Index | Name | Class | Number_Of_Students | Bus_Number
The names in the 'Name' column are listed like: Surname, Mx. Forename MiddleInitial
I am trying to split the 'Name' column into two separate columns: Surname | Forename
This is what I have tried:
ChaperoneName = df['Name']
df = df.assign( Surname = ChaperoneName[0], Forename = ChaperoneName[2])
The idea was if I isolated the one column, I could slice the strings in each row to get the Surname and the Forename into their new respective columns. But this has not worked for me. All I get in return are 2 properly named columns, but with the 0 and 2 indexed names repeating in each row.
CodePudding user response:
Try this:
df['Surname'] = df['Name'].apply(lambda x: x.split(",")[0])
df['Forename'] = df['Name'].apply(lambda x: x.split(",")[1])