20

I have a pandas dataframe containing (besides other columns) full names:

 fullname
 martin master
 andreas test

I want to create a new column which splits the fullname column along the blank space and assigns the last element to a new column. The result should look like:

 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 a KeyError: -1

I use [-1], that is the last element of the split group, in order to be sure that I get the real last name. In some cases (e.g. a name like andreas martin master), this helps to get the last name, that is, master.

So how can I do this?

musically_ut
  • 34,028
  • 8
  • 94
  • 106
beta
  • 5,324
  • 15
  • 57
  • 99

2 Answers2

41

You need another str to access the last splits for every row, what you did was essentially try to index the series using a non-existent label:

In [31]:

df['lastname'] = df['fullname'].str.split().str[-1]
df
Out[31]:
         fullname lastname
0   martin master   master
1    andreas test     test
EdChum
  • 376,765
  • 198
  • 813
  • 562
  • Similar thread https://stackoverflow.com/questions/12504976/get-last-column-after-str-split-operation-on-column-in-pandas-dataframe/44976922#comment109692323_44976922 – chanduthedev Jun 19 '20 at 06:00
4

If need create 2 new columns, use str.rsplit with parameter n=1. If need only last column, EdChum solution is better:

print (df)
                fullname
0          martin master
1           andreas test
2  andreas martin master

df[['first_name','last_name']] = df['fullname'].str.rsplit(expand=True, n=1)
print (df)
                fullname      first_name last_name
0          martin master          martin    master
1           andreas test         andreas      test
2  andreas martin master  andreas martin    master
Community
  • 1
  • 1
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252