I have a pandas framework containing (among other columns) full names:
fullname
martin master
andreas test
I want to create a new column that splits the column of the full name along the space and assigns the last element to the new column. The result should look like this:
fullname lastname
martin master master
andreas test test
I thought it would work like this:
df['lastname'] = df['fullname'].str.split(' ')[-1]
However i get KeyError: -1
I use [-1], that is, the last element of the divided group, to make sure that I get the real last name. In some cases (for example, a name of the type andreas martin master) this helps to get the last name, that is, the master.
So how can I do this?
source
share